Since I started using email extensively a few years ago, my overall mail archive grew to around 100,000 messages. This is not much for my email system, but too much for my mail provider, whose storage limit I just hit.

So I want to delete messages on the remote side, but keep them locally. Very occasionally I login to the web frontend of my mail provider to fetch a mail, so I don’t want to have all mails deleted. Just the ones that are older then three months.

For IMAP synchronization I use offlineimap, which offers the config option postsynchook for providing a shell script that is run after the sync. In this script, an additional program is called, that understands how old a specific mail is and moves the old mail into an archive folder, which offlineimap doesn’t sync. As I use notmuch for tagging my mail, I also use afew for initial tagging of my mail. For example, afew adds the tag lists/nix-devel to mails of the Nix development mailing list. Afew also offers a MailMover filter, that moves files from one maildir to another. The configuration that goes into ~/.config/afew/config could look like the following:

folders =
# move everything older than two months to local = 'date:..3-months'

First part of the MailMover configuration is the folder setting. Here I state, from which folder mails are moved. As I use multiple mail accounts, whith all of them having their mail in ~/Mail/ I seperate them in folders named after the corresponding mail address. The INBOX maildir of is therefore in ~/Mail/, not in ~/Mail/INBOX. Change the location according to your setup.

Second, I declare a rule of what is supposed to be moved to which place. The syntax here is

<source> = <notmuch-query>:<destination>

. My notmuch query gets all mail that is older than three months and reside in the right folder (that might be unnecessary, as afew seems to move only from the source folder, I just double checked here). The destination is a new local maildir, that doesn’t get synced by offlineimap.

afew runs the MailMover action only when being called as afew --move. By putting that command into my postsynchook, offlineimap calls afew every time I fetch my new mail. Every month, that command triggers the move of mail, that doesn’t belong into my remote storage anymore.

Documentation about the MailMover filter can be found here.