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.


Note: I am using Debian 7.8 with Postfix 2.9.6 & Dovecot 2.1.7


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
  • But I think for my purpose sentbefore works better, since savedbefore returned wrong or even no results. EDIT: As it seems, since I copied back a backup recently, all the "saved" dates were reset to that day, which is reason for this rather weird behaviour. I also wanna add, that using the SEEN parameter might be useful, to exclude unread e-mails. – comfreak Aug 9 '15 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.