Remove empty folders in offlineimap

Thu 31 January 2019
By makefu

Offlineimap becomes quite slow when you have so many (empty folders), this is how you can delete them.

The blog post is based on a (now offline) article by Matt Palma.


Besure to double and triple check every command, actions on the remote are permanent. If you want to read more about the dangers of deleting stuff, read the Matt Palmers article.

Find all empty folders

cd Mail/ # or wherever you put your Mails
for i in INBOX.*; do find "$i" -type f | grep -q "$i" || \
  (echo "$i" ); done >/tmp/empties
cat /tmp/empties

Review the file list

Remove empty folders locally

cat /tmp/empties | while read i ;do
  rm -rvf "$i"

The code snippet supports folders with spaces.

Remove empty folders via offlineimap remotely

This is where we use offlineimap instead of logging into the remote server.

cat /tmp/empties | while read i ;do
  offlineimap --delete-folder "$i"