SDB:Cleaning your IMAP account
Getting your e-mails
As already mentioned, first of all, you'll need to get your IMAP folder in the Maildir format on your hard drive. This is not so hard as some the Mail clients uses Maildir to store your e-mails. If your prefered mail client doesn't or if you don't know how, you can use offlineimap to get the synchronization between your local Maildir and remote IMAP.
Basic offlineimap configuration
You can start with some minimal configuration like the following one:
# Sample minimal config file. Copy this to ~/.offlineimaprc and edit it [general] accounts = Test [Account Test] localrepository = Local remoterepository = Remote [Repository Local] type = Maildir localfolders = ~/Mail [Repository Remote] type = IMAP remotehost = examplehost remoteuser = geeko remotepass = supersecret
Then you just need to run offlineimap. It will download all your mails and you'll have them in Maildir on your hard drive. Once you'll be done with filtering, you probably wants to run it again to synchronize your changes back to the server.
Filtering
We want to find some mails. We will use find to do so. This utility will help us to get all files. But we want only some of them, so will use grep to filter which we want and which not. And we want to delete selected ones. Let's take a look at few examples. Generally we will use following command:
find ~/Mail -type f -exec grep -l "any character pattern you would like to find goes here" \{\} \; -delete
It says, that we want find everything in ~/Mail directory, what is file (-type f), for all files we want to test them with grep whether they contain expresion we are looking for (-exec grep -l "expresion" \{\} \;) and we want to delete these that do (-delete).
Example 1
find ~/Mail -type f -exec grep -l "mailing-list@somewhere-you-do-not-subcribe-anymore.com" \{\} \; -delete
This will remove any email containing "mailing-list@somewhere-you-do-not-subcribe-anymore.com".
Example 2
find ~/Mail -type f -exec grep -li "viagra" \{\} \; -delete
This will remove all e-mail containing word viagra. Extra i in grep means that search will be case insensitive so it will match even ViAGrA.
Please type
man find man grep
to learn more about different options you might enjoy. Hint: you can use this to clean your home directory as well.