Buy Me a Coffee? Your support is much appreciated!
import time
from google_apis import create_service
CLIENT_FILE = 'client-secret.json'
API_NAME = 'gmail'
API_VERSION = 'v1'
SCOPES = ['https://mail.google.com/']
gmail_service = create_service(CLIENT_FILE, API_NAME, API_VERSION, SCOPES)
# step 1. Serach emails
def search_emails(query, labels=None):
# email_messages = []
next_page_token = None
message_response = gmail_service.users().messages().list(
userId='me',
labelIds=labels,
includeSpamTrash=False,
q=query,
maxResults=500
).execute()
email_messages = message_response.get('messages')
next_page_token = message_response.get('nextPageToken')
while next_page_token:
message_response = gmail_service.users().messages().list(
userId='me',
labelIds=labels,
q=query,
maxResults=500,
includeSpamTrash=False,
pageToken=next_page_token
).execute()
email_messages.extend(message_response['messages'])
next_page_token = message_response.get('nextPageToken')
print('Page Token: {0}'.format(next_page_token))
time.sleep(0.5)
return email_messages
query_string = "subject:Delete This "
email_results = search_emails(query_string)
# Step 2. delete emails
for email_result in email_results:
gmail_service.users().messages().trash(
userId='me',
id=email_result['id']
).execute()

but how to delete email from particle label
for example forums label
or particular date range and sent from particular email
for example
after:2023/4/12 lable:forums
from:(notifications@github.com) after:2023/3/10 before:2023/4/22
Good question! I need this aswell.
you can change the query according to what you want… for above e.g., use after:2023/4/12 lable:forums in the query_string like query_string = “after:2023/4/12 lable:forums”
from google_apis import create_service
ModuleNotFoundError: No module named ‘google_apis’
I am facing this issue!