Skip to content

Instantly share code, notes, and snippets.

@mcginleyr1
Last active December 15, 2015 19:29
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 mcginleyr1/5311874 to your computer and use it in GitHub Desktop.
Save mcginleyr1/5311874 to your computer and use it in GitHub Desktop.
Delete all contents of a Gmail Folder. Good for cleaning up overloaded folders.
import imaplib
import optparse
def delete_mail(email, password, label, search):
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(email, password)
mail.select(label)
typ, data = mail.search(None, search)
for num in data[0].split():
print mail.store(num, '+FLAGS', '\\Deleted')
if int(num.strip()) % 100.0 == 0.0:
print "EXPUNGE"
mail.expunge()
mail.expunge()
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-e', '--e-mail', dest='email', help='Email address to delete from.')
parser.add_option('-p', '--password', dest='password', help='Email addresses password.')
parser.add_option('-l', '--label', dest='label', default='Spam', help='Label where data we want to delete is located (Inbox, Alerter, Alert).')
parser.add_option('-s', '--search', dest='search', default='ALL', help='Optional search param to reduce deletes.')
(options, args) = parser.parse_args()
delete_mail(options.email, options.password, options.label, options.search)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment