Expunging expired Junk and Trash emails with dovecot

Of course we want to delete old Junk and Trash emails to save hard disk space. To do that, with dovecot 2.0, you don't have to enable the expire-plugin, unless you have to manage a very big server, in which case you may consider to enable it in conjunction with the dictionary service, to store the timestamps in a database.

This command

doveadm expunge -A mailbox Junk savedbefore 60d

will do a connection to the userdb, sql/MySQL in our case, and iterate in all (-A option) user's mailbox looking for expired emails, moved to the Junk folder more than 60 days ago. Be aware that the messages saved to Inbox 60 days ago and moved to Junk today will not be deleted.

To achieve the purpose it will be sufficient to create a shell script like this

> nano /usr/local/dovecot/etc/dovecot_expunge.sh

#!/bin/bash
#
DOVEADM="/usr/local/dovecot/bin/doveadm";

$DOVEADM expunge -A mailbox Trash savedbefore 90d
$DOVEADM expunge -A mailbox Junk  savedbefore 60d

assign the +x priviledge:

chmod +x /usr/local/dovecot/etc/dovecot_expunge.sh

and run the script once a month or whatever as a cronjob

> crontab -e

# dovecot delete spam & trash
#minute hour mday month wday command
40 3 12 * * /usr/local/dovecot/etc/dovecot_expunge.sh

What to do when using the vpopmail auth driver

Unfortunately, the vpopmail's APIs seem not to provide an iteration feature. The good news is that Costel Balta has a brilliant solution. You can download his script here.

#!/bin/bash
#
# Author: Costel Balta
# Slightly modified by Roberto Puzzanghera
#

# MySQL details
HOST="mysql-server-IP";
USER="vpopmail";
PWD="vpopmailpassword";
MYSQL="/usr/local/mysql/bin/mysql";
# dovecot details
DOVEADM="/usr/local/dovecot/bin/doveadm";

# Output sql to a file that we want to run
echo "USE vpopmail; select concat(pw_name,'@',pw_domain) as username from vpopmail;" > /tmp/query.sql;

# Run the query and get the results (adjust the path to mysql)
results=`$MYSQL -h $HOST -u $USER -p$PWD -N < /tmp/query.sql`;

# Loop through each row
for row in $results
        do
        echo "Purging $row Trash and Junk mailbox..."
        # Purge expired Trash
        $DOVEADM -v expunge mailbox Trash -u "$row" savedbefore 90d
        # Purge expired Junk
        $DOVEADM -v expunge mailbox Junk  -u "$row" savedbefore 60d
done

This script does a mysql query selecting all users from the vpopmail's database, stores the results in a variable and iterates through each user's mailbox deleting old emails from Trash and Junk folders. Since this script stores the mysql access it must be runned by root and must have root's read priviledges.

If you want to avoid email notifications about this task, because you have tons of users, simply comment the echo inside the script.

cd /usr/local/dovecot/etc
wget http://notes.sagredo.eu/sites/notes.sagredo.eu/files/qmail/dovecot_expire
mv dovecot_expire dovecot_expire.sh
chown root.root dovecot_expire.sh
chmod 0700 /usr/local/dovecot/etc/dovecot_expire.sh

Run the script once a month or whatever as a cronjob

> crontab -e

# dovecot delete spam & trash
#minute hour mday month wday command
40 3 12 * * /usr/local/dovecot/etc/dovecot_expire.sh

Comments

Other folders

Hi, what about other folders?

When you mark a message as deleted in INBOX folder?

I think you should go through all user folders :)

Best,

Rafal.

Rafal, what do you mean by

Rafal, what do you mean by "mark a message as deleted in INBOX folder"?

Hi Roberto,I wasn't clear

Hi Roberto,

I wasn't clear before, I am sorry. Please look at this example:

1) A new message (Seen Flag)

root@mail J:0 S:1 Maildir# tree {cur,new,tmp}
cur
└── 1417989074.M205847P32064.XXX,S=2582,W=2640:2,S
new
tmp

0 directories, 1 file

2) The message is still in INBOX folder but has got a "Trashed" flag

root@mail J:0 S:1 Maildir# tree {cur,new,tmp}
cur
└── 1417989074.M205847P32064.XXX,S=2582,W=2640:2,ST
new
tmp

0 directories, 1 file

3) I tried to expunge deleted messages from Trash folder

root@mail J:0 S:1 # doveadm expunge -u XXX mailbox Trash savedbefore 1s deleted

4) The message is still in INBOX folder (still uses storage)

root@mail J:0 S:1 Maildir# tree {cur,new,tmp}
cur
└── 1417989074.M205847P32064.XXX,S=2582,W=2640:2,ST
new
tmp

0 directories, 1 file

5) I deleted all messages with "Trashed" flag from INBOX folder and..

root@mail J:0 S:1 Maildir# doveadm expunge -u XXX mailbox INBOX savedbefore 1s deleted

6) The message has been removed from the hard-drive

root@mail J:0 S:1 Maildir# tree {cur,new,tmp}
cur
new
tmp

0 directories, 0 files

If a mail reader doesn't support "Expunge" every folder on exit, you will have a lot of deleted emails that use your storage and they are still in dovecot's index file which affects performance...

Your script is good but you should consider to "clean" users folders from deleted messages which have not been moved to Trash or Junk folders (like savedbefore 60d).

I would also change this line:

$DOVEADM -v expunge mailbox Trash -u $row savedbefore 90d

to:

$DOVEADM -v expunge mailbox Trash -u "$row" savedbefore 90d

or

$DOVEADM -v expunge mailbox Trash -u "${row}" savedbefore 90d

If somehow in user's name is a spacebar that would "crash" the script... and it is vulnerable to inject user's code (eg. read other users emails).

I hope you won't consider my message as something bad or negative. These are just my conclusions. You can always delete my comment :)

Best,

Rafal.

how do you get that tag?

how do you get that Trashed tag? I expect that as soon as you delete the message by your client it is moved to .Trash

PS: I've never missed to reply/publish a comment, when it contains interesting things to learn. And I think this is the case, so thanks to you :)

Hi Roberto,I have been busy

Hi Roberto,

I have been busy recently...

Try for example in Thunderbird to delete message by pressing Shift + Delete when a message is highlighted. You can do the same through telnet and other mail clients.

I hope it helps.

Best,

Rafal.

Ok, I see. But I can't find a

Ok, I see. But I can't find a way to iterate over alla mailboxes...

any help?

There is a simple solution

There is a simple solution when using vpopmail:

40 4 12 * * for user in $(/home/vpopmail/bin/vpopbull -nV); do /usr/local/bin/doveadm -v expunge -u $user \( mailbox Trash OR mailbox Junk \) SAVEDBEFORE 8w ; done