All site contents ©2024 Casey Connor unless otherwise noted.

Bulk IMAP deletion of attachments from emails in a folder: IMAP Size Reducer

Last Updated: Thursday, 14 September 2023

This is a python script that deletes all attachments and inline media from emails that are over a certain size in a specified folder.

Download the script here. Current version: 1.1

I'm afraid I can not offer support for this software, but feel free to get in touch if you have trouble and if I have time I might respond. Corrections to the code are also welcome!

Donations are welcome!

USE AT YOUR OWN RISK!

I MAKE NO GUARANTEES WHATSOEVER ABOUT THE FUNCTIONING OF THIS SOFTWARE!

THIS MIGHT DELETE YOUR EMAIL! MAKE BACKUPS IF YOU DON'T KNOW WHAT YOU ARE DOING!

When you want to reduce the size of your IMAP folders, one solution is to delete the attachments you no longer need from messages. Most modern IMAP clients provide some way to do this, but most (e.g. Thunderbird, my preferred client) can not do it for multiple messages at once, and add-ons that used to do this are no longer supported or functional. If you have hundreds or thousands of messages to go through, doing this by hand for each message is out of the question.

There is an old windows utility called IMAPSize which works OK, but is no longer supported, crashes frequently, and when I used it, left behind a number of messages with attachments that for whatever reason it believed had no attachments.

If you are a GMail user, you might look into Unattach -- I can't vouch for it, but it looks like it should work.

But for most of us it seems there is currently no easy way to mass delete email attachments, so I went looking for scripts. I found a couple, but wanted to tweak them to suit my needs, hence this script.

Notes:

To do a "test run", answer "y" to the first question. This will do everything except write the new message back to the server and delete the old message, so it should be safe.

If you CTRL-C or send a SIGTERM it should fail gracefully (i.e. finish with the current message and then logout from the server.)

You might want to expunge (aka "compact") the folder first, as this script will also find and modify deleted-but-not-expunged messages.

If the message can not be reduced, it will not be altered on the server.

Sometimes IMAP account names are of the form "accountname", and sometimes they are of the form "accountname@server.com". It will depend on your email provider.

The same is true for the naming of your IMAP folders. In my case, "INBOX.Sent" was what I wanted, but it's not always obvious.

This will make an SSL IMAP connection using the imaplib.IMAP4_SSL() call. I'm not sure what the capabilities of that library are -- it's possible that some servers will have encryption or authentication methods that it doesn't support.

Note that if a message is very large but it has no attachments (e.g. a very long text message) or has many small parts that are all under the "tolerable part size" you specify, the message will not be reduced in size.

The following MIME types will be candidates for part deletion:

  • Anything with Content-Disposition "attachment"
  • Content-Type image/*
  • Content-Type audio/*
  • Content-Type video/*
  • Content-Type music/*
  • Content-Type x-music/*
  • Content-Type application/*

Example Run:

$ ./imap_size_reducer.py
This program will delete all attachments AND inline images, etc, from messages, if those individual parts are above a tolerable lower size limit and the overall message is above a max size limit.
NOTE: you might want to expunge (aka "compact") the folder first, as this script will also find and modify deleted-but-not-expunged messages.

Is this a test run only? [Ny]:
Enter IMAP server for connection: imap.yourserver.org
Enter login for IMAP server: accountname
Enter password for accountname at imap.yourserver.org
Password:
Enter folder name: INBOX.Sent
Enter max desired message size in integer KiB (or hit enter to delete attachments from all messages in specified folder): 150
Enter tolerable part size in integer KiB (individual message attachments under this size will not be deleted; hit enter for 10KiB, enter 0 to delete all attachments):

Logging in...
Login successful. Selecting mailbox...
Folder INBOX.Sent selected. Searching for messages over 150.0KiB...
85 message(s) found
Proceed with deletion of attachments? [Ny]: y
--------------------
Deleting attachments for message uid 14
[Subject]: [This is the example subject of the first large email!]
[Date]: [Wed, 11 Feb 2004 21:24:58 ]
message size before reduction: 153.0185546875KiB
Deleting attachment: application/octet-stream
message size after reduction: 8.12109375KiB
saving new message to server...
flagging old as deleted...
done with message; old uid: 14
--------------------
Deleting attachments for message uid 165
...
...(many log lines ommitted)
...
--------------------
Deleting attachments for message uid 12686
[Subject]: [Re: Fwd: Another large email message subject line...]
[Date]: [Mon, 06 Jan 2014 20:26:09 -0800]
message size before reduction: 250.4990234375KiB
message size after reduction: 250.4990234375KiB
Message could not be reduced with current settings; will not change on server.
done with message; old uid: 12686
--------------------
Deleting attachments for message uid 26263
[Subject]: [Re: Some other large email subject line...]
[Date]: [Thu, 10 Sep 2020 16:05:39 -0700]
message size before reduction: 332.8642578125KiB
Skipping small attachment: image/png
Skipping small attachment: image/png
Skipping small attachment: image/png
Skipping small attachment: image/png
Skipping small attachment: image/png
Skipping small attachment: image/png
message size after reduction: 332.8642578125KiB
Message could not be reduced with current settings; will not change on server.
done with message; old uid: 26263

expunging any deleted messages in folder...
72 message(s) reduced in size. 9126.47KiB has been freed (assuming messages are expunged.)
Closed folder.
Logged out of server.