How to automatically delete email messages in the server using Courier-IMAP

Today my boss asked me whether it is possible to automatically delete email messages in the server inside certain folders after a given number of days. For example he wants the sent emails inside the ‘Sent’ folder is automatically deleted after 2 days. And also in the ‘Trash’ folder as well.

The answer is yes. It is possible to do it and easy. If you are using Courier-IMAP (a server that provides IMAP access to Maildirs), by default the email messages inside ‘Trash’ folder will be deleted after 7 days. It is set by the

IMAP_EMPTYTRASH=Trash:7 

option in the /path/to/courier-imap/etc/imapd file. In Centos, the file is /usr/lib/courier-imap/etc/imapd.

IMAP_EMPTYTRASH is a comma-separated list of folder:days.

So if we want to automatically delete email messages inside the ‘Trash’ and ‘Sent’ folder after 2 days old, set the IMAP_EMPTYTRASH value as follow:

IMAP_EMPTYTRASH=Trash:2,Sent:2

Of course you can do the same thing to other folders. Folder Drafts for example set to delete the messages after 7 days. The value becomes as follow:

IMAP_EMPTYTRASH=Trash:2,Sent:2,Drafts:7

Restart the Courier-IMAP service to re-read the changes.

/etc/init.d/courier-imap restart