If you need to expunge an email received by many users, it is far easier to do this directly in the mail store instead of using an email client to login as each user, search/find/delete, repeat, repeat, etc.
OSX Server has used a couple different versions of Dovecot, which is where email has been stored since Apple abandoned use of Cyrus for this purpose.
|OSX||Server Admin version||Dovecot version|
|Yosemite 10.10||(Need to fill in the blanks for newer Dovecot and OSX versions)|
|Mountain Lion 10.8|
|Lion 10.7||2.0.13||http://blog.magnusviri.com/os-x-server-dovecot.html||Introduces doveadm and other necessary admin tools|
|Snow Leopard 10.6||10.65||1.1.20apple0.5||
User directories: /var/spool/imap/dovecot/mail
Dovecot 1 in Snow Leopard 10.6
Dovecot 1.x does not have the nifty Dovecot admin tools available to version 2+, but good things can still be done.
Search the entire mail store for emails containing specific text and list the files
Dovecot is uses the MailDir mailbox format where each email is stored in a separate file. This allows us to use command line tools to search for content.
sudo grep -rl --exclude "dovecot*index*" "search-text" /private/var/spool/imap/dovecot/mail
This can take a very very long time to search a huge mail store.
You can reduce to search specific user mailboxes by narrowing the search path. Server Admin displays the path for each users' mail store, so if you're confronted with cryptic directory names like "61E7D036-335D-4838-9C0E-F25A6133FC7C", find the directory corresponding to the user in Server Admin.
Delete email directly from the mail store
Find the emails in the mail store by searching as documented above. You'll get a list of emails in the mail store. Just delete the email as root using "sudo rm".
Be advised that the grep results above do not escape any spaces in OSX paths, so you have to do this yourself. TextWrangler is your friend: paste the grep results from above, search for (space) and replace with backslash-space, ie "\ ". If you don't properly escape the spaces, you'll see the error: "No such file or directory".
According to this article, it's safe to remove individual emails by simply deleting the files from the server. Dovecot should see the directory was updated and automatically trigger a re-index.
Manually delete old email from user mailboxes
If you have an email retention policy that allows for older emails to be deleted, it can be practical to reduce the size of IMAP mail stores which may have become bloated over the years.
Dovecot v1 has a means to do this: the Expire plugin, but Apple in its wisdom did not compile Dovecot with support for DB or SQL driver support, so it may not be used.
> sudo /usr/sbin/dovecotd --build-options
Build options: ioloop=kqueue notify=kqueue ipv6 openssl
Mail storages: maildir mbox dbox cydir raw
Passdb: checkpassword od pam passwd passwd-file
Userdb: od passwd passwd-file prefetch static
If you try to use this you will get errors like:
Error: read(/private/var/run/dovecot/dict-server) failed: Remote disconnected
8/14/15 1:11:04 PM dovecot dict: dict client: Unconfigured dictionary name 'expire'
Since it's OK to delete individual emails directly from the mail store (see above), we can accomplish this directly from the file system. You might want to be sure you have a backup of the mail store before you do this!
The following deletes all emails from the Snow Leopard mail store older than 365 days:
> sudo find /var/spool/imap/dovecot/mail -mtime +365 -type f -delete
To preview how many emails will be deleted:
> sudo find /var/spool/imap/dovecot/mail -mtime +365 -type f | grep -c /
If the server has a ton of ancient email on it, you might want to restrict this to just the largest mailboxes or deleting email 10 years old and working your way to the present, etc.
Be very careful to use full and NOT relative paths, as a typo may cause you to delete your server.
Automatically delete old email from user mailboxes
After you have done the above, you can cause deletion to occur on a daily basis by setting up a cron job or launchd to execute daily.
Delete old email from user mailboxes