How to delete messages from Gmail using python IMAPclient ?

Google does not delete all messages at once according to
So, first, you have to delete messages in each folder:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from imapclient import IMAPClient

client = IMAPClient('', use_uid=True, ssl=True)
client.login('', 'password')

folders = [
    '[Gmail]/Sent Mail',

for f in folders:
    fold = client.select_folder(f)
    res = client.delete_messages(
    res = client.expunge()

#Google automatically will move deleted messages to "All Mail" folder. 
#Now we can remove all messages from "All Mail"

client.select_folder("[Gmail]/All Mail")
client.set_gmail_labels(, '\\Trash')

More information on how to work with Gmail using python IMAPClient can be found directly at
Read the source code, Luke 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *