Tuesday, October 18, 2016

Delete outlook mails

Unfortunate chain of events during some testing conducted by my colleague led to a full folder in my outlook. When I say full I mean ~7M emails generated during test that lasted a couple of minutes.

Python to the rescue (outlook needs to run during this operation)...
 
import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders["testnotifications"].Folders["Inbox"]

messages = inbox.Items
for message in messages:
    print("Deleting a message %s" % message.Subject )
    message.Delete()
deleted = outlook.Folders["testnotifications"].Folders["Deleted Items"]

while True:
    message = deleted.Items.GetLast()
    print("Deleting a message %s" % message.Subject)
    message.Delete()
print("Done")