Dovecot. Autoclean email in Trash

I will give an example of automatic deletion of emails that are in the Trash and Junk/Spam folder, by the way, you can delete emails in other folders in the same way.

For example, I’ll take a mail server with iRedAdmin that uses Dovecot. To make sure the emails are cleaned up, open any old mailbox that has emails in the Trash folder and, for example, run the command below to delete emails older than 5 weeks:

doveadm expunge -u mailbox Trash savedbefore 5w

If the emails are deleted, then everything is fine and you can make a script (for example, to clear all mailboxes of emails in the Trash older than 30 days and the Spam folder older than 90 days):

# which doveadm
$DOVEADM expunge -A mailbox Trash savedbefore 30d
$DOVEADM expunge -A mailbox Junk  savedbefore 90d

Let’s make the script file executable (the script can be placed in any convenient place):

chmod +x /mydir/scripts/

To make the script automatically run, for example at midnight at 10 minutes, add the line below to /etc/crontab:

10 0 * * * root /mydir/scripts/ >/dev/null 2>&1

If there are few emails, then you can do it once a month, for example, on the first day:

10 0 * * 1 root /mydir/scripts/ >/dev/null 2>&1

See my other articles about the mail server
Using and configuring CRON

Leave a comment

Leave a Reply