How to delete X days old email

General discussion about Zimbra Desktop.
bhairavdhanwade
Posts: 1
Joined: Wed Jul 29, 2015 8:06 am

How to delete X days old email

Postby bhairavdhanwade » Wed Jul 29, 2015 8:09 am

    ## This is the Fully automated script to check and delete one year older  mails. ##        

#!/bin/bash

ZIMBRA_BIN=/opt/zimbra/bin

THEDATE=$(date -d "-365 day" +"%m/%d/%y")
OneYearAgo=THEDATE
echo "One year ago : $THEDATE"
TODAY=$( date +"%m/%d/%y")


## Get E-mail Address List
/opt/zimbra/bin/zmprov -l gaa | grep "@domain.com" | sort > /usr/local/src/script/domain_email_id.txt
/opt/zimbra/bin/zmprov -l gaa | grep "@domain1.com" | sort > /usr/local/src/script/domain1_email_id.txt
cat /usr/local/src/script/domain_email_id.txt /usr/local/src/script/domain1_email_id.txt > /usr/local/src/script/e-mail-id.txt



while read THEACCOUNT
do
echo "Searching mail from $THEDATE to $TODAY for $THEACCOUNT:"
LOGF=`echo $THEACCOUNT | awk -F "@" '{print $1}'`
for i in `$ZIMBRA_BIN/zmmailbox -z -m $THEACCOUNT search -l 1000 "(before:$THEDATE)" | grep conv | sed -e "s/^ss*//" | sed -e "s/ss*/ /g" | cut -d" " -f2`
do
if [[ $i =~ [-]{1} ]]
then
MESSAGEID=${i#-}
echo "deleteMessage $MESSAGEID" >> /tmp/$LOGF.txt
else
echo "deleteConversation $i" >> /tmp/$LOGF.txt
fi
done

$ZIMBRA_BIN/zmmailbox -z -m $THEACCOUNT < /tmp/$LOGF.txt >> /tmp/process.log
rm -f /tmp/$LOGF.txt

done < /usr/local/src/script/e-mail-id.txt




User avatar
manfred.gipp
Advanced member
Advanced member
Posts: 51
Joined: Tue Feb 03, 2015 7:08 am
Location: Germany
ZCS/ZD Version: Zimbra 8.8.6_GA_1906 (build 2017113
Contact:

How to delete X days old email

Postby manfred.gipp » Wed Oct 07, 2015 9:31 am

I try to use nearly the same script to move automatically Mails from Inbox-Folder to another folder.

I use "moveMessage" and "moveConversation".

But this works not as expected.

In your case "deleteConversation" deletes all Mails concerning to this conversation.

This means for your script, that it could be that newer Mails are deleted.
polmstead
Posts: 1
Joined: Wed Dec 16, 2015 3:49 pm

How to delete X days old email

Postby polmstead » Wed Dec 16, 2015 3:59 pm

Thank you for this post. I'm new to scripting and was wondering if this could be modified to target specific folders. What I'm trying to accomplish is to target the trash and sent items folder and delete anything over 15 months old. Could you suggest how I could do this? Any help is appreciated. Thank you.
Erik-NA
Posts: 23
Joined: Wed Dec 14, 2016 1:06 pm
Location: Sweden
ZCS/ZD Version: Zimbra Collaboration 8.8.11

Re: How to delete X days old email

Postby Erik-NA » Sun Mar 03, 2019 5:28 pm

Hello
Modified the script a bit:
  • Can run as a cron job
  • Can now handle spaces in folder name
  • Using DeleteItem instead of DeleteMessage in zmmailbox since I got errors when trying to delete some messages using DeleteMessage

Todo: Better error handling from zmmailbox

Run as user zimbra.

Modify according to your needs :)

Code: Select all

#!/bin/bash
#
ZIMBRA_BIN=/opt/zimbra/bin

#Params: account folder days
#Account name
#Folder name
#Number of days for which older emails will be deleted
doPurge ()
{
    local _account="$1"
    local _folder="$2"
    local _days=$(eval "date --date='$3 days ago' +%m/%d/%y")
    local _rows=0
    local _totalRows=0
         
    rm -f /tmp/deleteOldMessagesList.txt
    rm -f /tmp/process.log
    while true; do
        _rows=0
        touch /tmp/deleteOldMessagesList.txt
        for messageId in $(eval "$ZIMBRA_BIN/zmmailbox -z -m $_account search -l 1000 'in:\"$_folder\" (before:$_days)' | grep conv | sed -e 's/^[ \t]*//' | tr -s ' ' | cut -d ' ' -f 2")
        do
            if [[ ! -z "$messageId" ]]; then
                echo "deleteMessage $messageId" >> /tmp/deleteOldMessagesList.txt
                _rows=$[$_rows+1]
            fi
        done
       
        if [ $_rows -gt 0 ]; then
            $ZIMBRA_BIN/zmmailbox -z -m $_account < /tmp/deleteOldMessagesList.txt >> /tmp/process.log
            rm -f /tmp/deleteOldMessagesList.txt
            _totalRows=$[$_totalRows + $_rows]
        else
            break
        fi
    done
    echo $_totalRows
}

for account in $(eval "$ZIMBRA_BIN/zmprov -l gaa")
do
    echo "Purging Emails for account $account"
    rows=$(doPurge "$account" "Activity Stream" "90")
    echo "Deleted $rows rows for account $account"
done
exit

Return to “General Questions”

Who is online

Users browsing this forum: No registered users and 8 guests