puzzling.org · mary.gardiner.id.au · Macquarie University

Python script to delete empty IMAP folders

This is a script that logs into an IMAP server and deletes all folders that do not contain messages.

Rationale: per Matt Palmer's blog entry OfflineIMAP and Deleting Folders users of any mail sorting recipe that creates new mail folders a lot tend to find that over time they accumulate a lot of mail folders for, eg, email lists they are no longer subscribed to. As do obsessive manual creators of new mail folders. And most IMAP clients will waste time checking those folders for new mail all the time.

If they empty said folders (as I do weekly with archivemail), this script is useful: it detects the empty folders and deletes them. Tada, no more endless empty old folders.

Warning: This script does not have extensive error handling and is, in fact, not well tested. It isn't safe against new mail being delivered to a folder in the time between checking it for messages and deleting it. The status is: I've run it against a couple of IMAP servers and did not delete any mail. Before using it, I suggest having a look over deleteEmptyIMAPFolders.py, which isn't very long, and verifying it for your own purposes. And also making a copy of your mail tree before running this for the first time. And, on general principle, having good backups anyway.

Requires: Python 2.3 or later.

Licence: MIT (Free Software). See individual files for more, they have individual copyright notices.

Download: you need two Python files:

  1. deleteEmptyIMAPFolders.py
  2. parseNested.py

For usage, run:

deleteEmptyIMAPFolders.py --help

You can also obtain the source using Bazaar:

bzr branch http://users.puzzling.org/users/mary/bzr/delete-empty-IMAP-folders/

Last modified: 22 July 2008