MailDir maintenance tips

Few tips on maintaining maildirs on the server:

To delete emails that are marked for deleting directly on the server:

find current_maildir_location/ -type f -name ‘*:2,*T*’ -mtime +7 -exec rm -f “‘{}'” ‘;’

This is helpful if users email client are sent to hide deleted messages and are not set to permanently delete them. Point is that maildir format has “T” flag in msg file name, which tells that msg is Trashed.

To archive old emails (something like 2 years old)

(cd current/ && find . -mtime +732 -print0) | rsync -ag –remove-source-files –files-from=- –from0 ./current_maildirs_location/ ./archive_maildirs_location/

The above will for recursively through all dirs in current_maildirs_location, find files older than 2 years and move them in archive_maildirs_location, preserving the directory structures.

Keep in mind that many mail clients do not handle large mailboxes well, so such tips can improve the speed for end-users, as well reduce storage requirements on the server (especially when talking about backups).