Delete old e-mails from a Maildir
I am currently trying to figure out how I can delete old e-mails in one folder of a Maildir installation on a Debian server. Basically what I want to do is to run a cron job every day or so that checks a specific folder in the Maildir for old e-mails and deletes them if they are older than a set amount of days.
Is there a simple way to filter e-mails from a Maildir by date or do I have to read in every e-mail as text, look for the timestamp, convert it, subtract the mentioned amount of days and then compare it to the current date?
I don't want to invent the wheel if there are already better wheels out there, so I would be glad if someone could help me out in doing this.
Thanks!
Note: I am using Debian 7.8 with Postfix 2.9.6 & Dovecot 2.1.7
Solution 1:
Since you have dovecot you can do that with doveadm(1).
For example deleting mail saved in the Trash folder for user before 30 days:
doveadm expunge mailbox Trash savedbefore 30d -u <user>
You can use -A flag for all users and if you want to check first for the mails that match use the search/fetch commands - see doveadm-search(1):
doveadm search mailbox Trash savedbefore 30d -u <user> |
while read guid uid; do
doveadm fetch -u <user> body mailbox-guid $guid uid $uid > msg.$uid
done