forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_emails.py
More file actions
19 lines (17 loc) · 732 Bytes
/
delete_emails.py
File metadata and controls
19 lines (17 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from common import gmail_authenticate, search_messages
def delete_messages(service, query):
messages_to_delete = search_messages(service, query)
print(f"Deleting {len(messages_to_delete)} emails.")
# it's possible to delete a single message with the delete API, like this:
# service.users().messages().delete(userId='me', id=msg['id'])
# but it's also possible to delete all the selected messages with one query, batchDelete
return service.users().messages().batchDelete(
userId='me',
body={
'ids': [ msg['id'] for msg in messages_to_delete]
}
).execute()
if __name__ == "__main__":
import sys
service = gmail_authenticate()
delete_messages(service, sys.argv[1])