Skip to content

Instantly share code, notes, and snippets.

@martinvirtel
Last active January 4, 2018 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinvirtel/2123ab01a1e32fa241db8a7908cf9bd1 to your computer and use it in GitHub Desktop.
Save martinvirtel/2123ab01a1e32fa241db8a7908cf9bd1 to your computer and use it in GitHub Desktop.
Mass Delete Imap Email (Python3 Script)
credentials.py
__pycache__

Mass Delete Imap Email (Python3 Script)

Sometimes you just want to clean up a very full imap mailbox programmatically. This Python3 script might help.

Be careful. With great power comes great responsibility.

You have to add a file named credentials.py for this to work. It should look like this:


credentials=dict(user="username",password="password")

... and it should contain your username and password. Then, configure the hostname in imapdel.py.

credentials=dict(user="username",password="password")
import imaplib
from credentials import credentials
with imaplib.IMAP4_SSL("imap.gmx.net") as M :
M.login(credentials["user"], credentials["password"])
M.select()
typ, data = M.search(None, 'NOT TO virtel@gmx.net BEFORE 01-jan-2013')
for num in data[0].split()[:10000]:
M.store(num, '+FLAGS', '\\Deleted')
print(f"{num} ... deleted ")
#typ, data = M.fetch(num, '(RFC822)')
#print(data)
M.expunge()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment