Skip to content

Instantly share code, notes, and snippets.

@dickermoshe
Created December 20, 2021 05:33
Show Gist options
  • Save dickermoshe/8fd68d67f9c59a683428adbe7554a061 to your computer and use it in GitHub Desktop.
Save dickermoshe/8fd68d67f9c59a683428adbe7554a061 to your computer and use it in GitHub Desktop.
This small python script will Permanently wipe emails based on a search phrase that is used on the Gmail website
import imaplib
# account credentials and other configs
# replace with you Gmail username and password
username = "replace with your Gmail username"
password = "replace with your Gmail password"
# Search query to delete
# Make sure to leave the single and double quotes
# Example >>> gmail_search = '"from:me"'
gmail_search = '"replace with your Search Phrase"'
folderToDeleteEmailsFrom = '"[Gmail]/All Mail"'
trashFolder = '[Gmail]/Trash'
# create IMAP4 with SSL
imap = imaplib.IMAP4_SSL("imap.gmail.com", 993)
# authenticate
imap.login(username, password)
# list all the mailboxes present
print(imap.list())
# SECTION 1: select the mailbox to delete emails from
imap.select(folderToDeleteEmailsFrom)
typ, [msg_ids] = imap.search(None, 'X-GM-RAW', gmail_search)
msg_count = len(msg_ids)
print("Found message count: ", msg_count)
if msg_count == 0:
print("No new messages matching the criteria to be deleted.")
else:
if isinstance(msg_ids, bytes):
# if it's a bytes type, decode to str
msg_ids = msg_ids.decode()
# SECTION 2: imap store command allows us to batch perform an operation
# on a bunch of comma-separated msg ids
msg_ids = ','.join(msg_ids.split(' '))
print("Moving to Trash using X-GM_LABELS.")
imap.store(msg_ids, '+X-GM-LABELS', '\\Trash')
# SECTION 3: Once all the required emails have been sent to Trash,
# permanently delete emails marked as deleted from the selected folder
print("Emptying Trash and expunge...")
imap.select(trashFolder)
imap.store("1:*", '+FLAGS', '\\Deleted') # Flag all Trash as Deleted
imap.expunge()
# SECTION 4: close the mailbox once the task is done
print("Done. Closing connection & logging out.")
imap.close()
# logout
imap.logout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment