Archivemail is a tool for archiving or deleting old mail in mailboxes. It moves or deletes messages older than the specified number of days. Combined with cron, it’s the perfect tool for keeping constantly growing mail folders like spam or trash under control.
I use IMAP hosted with Pair Networks, but the discussion that follows should be broadly applicable to any remote or local mail server. For purposes of illustration, let’s assume we have the domain “example.com”, our hosting account’s username is “account”, and the mailbox we will be working with is “email@example.com”. Pair Networks uses mbox format, but to the best of my knowledge the following works equally well with maildir format.
To illustrate the use of archivemail, let’s give ourselves the task of automatically deleting messages in the Sent folder that are more than 30 days old.
Archivemail is available through the software repositories of most distributions and comes pre-installed on many web hosts.
First, test your syntax using the dry run and verbose options:
archivemail --dry-run --verbose --delete --days=30 /path/to/folder
The paths and names of your mail folders will vary from system to system. To provide two examples, on my remote mail server (using BSD, IMAP and mbox) the sent mail folder is:
and on my local box (using Linux, Evolution, IMAP, and mbox) it is:
Inspect the output of the dry run. If all is well, replace the options –dry-run and –verbose with –quiet and set it up as a daily cron job.
Consider applying the same technique to other mail folders that can benefit from cleaning, such as INBOX.Spam and INBOX.Trash. So long as the archivemail options being used are identical, you can combine multiple mail folders or boxes in a single command using the syntax archivemail [options] mailbox_1 mailbox_2…