5

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

comfreak
  • 1,451
  • 1
  • 21
  • 32

1 Answers1

8

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
nkms
  • 316
  • 2
  • 4
  • 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 09 '15 at 20:30