Luke Cyca .com

IMAP Mass-delete

You know how when a meltdown happens and you get tens of thousands of automated emails telling you about it? And then Mail.app cringes as you try to select them all and delete them? No? Then we do different work.

But if it ever does happen to you, you can use this python code snippit to connect directly to your mail server and delete them. I usually just paste this kind of thing right into the python interpreter.

The (FROM \"zabbix@example.com\") part is the selection criterion in this example, but there are more to choose from!

import imaplib
from getpass import getpass

imap = imaplib.IMAP4_SSL('mail.example.com')
imap.login('luke',getpass())

imap.select()
typ, data = imap.search(None, '(FROM \"zabbix@example.com\")')
for num in data[0].split():
imap.store(num, '+FLAGS', '\\Deleted')

imap.expunge()
imap.close()
imap.logout()
Luke Cyca .com