Expunging emails with dovecot

Of course we want to delete old Junk and Trash emails to save hard disk space.

If you want to expunge your Junk and Trash folder after 60 days you can set 15-mailboxes.conf as follows:

namespace {
  mailbox Junk {
    special_use = \Junk
    auto = subscribe
    autoexpunge = 60d
  }

  mailbox Trash {
    special_use = \Trash
    auto = subscribe
    autoexpunge = 60d
  }
}

Be aware that the messages saved to Inbox 60 days ago and moved to Junk today will not be deleted.

In case you need to act more precisely on your mailboxes you may want to take a look at this script by Tony Fung.


I leave intact these following notes for those who are not satisfied with the previous method and prefer to set up an expunge script to be run via cronjob. Those who set up the autoexpunge as explained above can jump to the next page.

This command

doveadm expunge -A mailbox Junk savedbefore 60d

alternative for clear mailbox of user

doveadm purge -u user@domain.com

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.

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

Katen Doe

Katen Doe

Hello, I’m a content writer who is fascinated by content fashion, celebrity and lifestyle. She helps clients bring the right content to the right people.

Press ESC to close