Last active
December 15, 2015 19:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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