Expunging expired Junk and Trash emails with dovecot
- More info: http://wiki2.dovecot.org/Plugins/Expire
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
There is a simple solution
There is a simple solution when using vpopmail: