So my work account which is powered by Gmail had over 300k email in each folder and I soon ran out of space. I figured there are tons of email from system alert which I no longer need. I searched for all required email and tried to bulk delete them. I got “oops the system encountered a problem (#001)” which is probably because google cannot handle bulk delete beyond a certain limit. There is no documentation on this limitation as well. up with google didn't help either. Hence I decided to explore gmail api and get it done using a python script. I was able to reduce memory used from 95% to 28% by running the script for about 2 hours. You should be able to do the same.
Before you start you should know how to search email using search . For example if you want to list all emails from firstname.lastname@example.org then you must type from:email@example.com in your Gmail search bar as shown in the screen shot
or if you want all emails under folder "online-assignment” you can query as per search box as shown below
So the idea is you should be able to search emails you want to delete using search box , the we will discuss shortly will accept this search box query and token to mass delete email. Please note that we will use temporary token from which is active for 1 hour after that we have to generate a new token. We can get a permanent token by making a google developer account and registering our account for authentication but that we will explore some other day and not needed for our current use case in hand Our script will stop working after 1 hour once the token expires and then you reinitiate the with a new token by repeating all the steps to get the previous token (will come to that shortly). Once all emails are deleted meeting the search , the script gives a massage as “no more mails to delete” and stops automatically.
Please note that the deletion is permanent and cannot be undone. So make sure your search query is correct and searching exact mails which you want to delete. You can refresh Gmail to see mail counts dropping to keep track of progress.
Getting the API token
Visit and select the scope as as shown in the screenshot and click on authorize APIs. The scope you selected allows deletion of email , any other scope will have different permission so make sure to select the scope correctly as mentioned here. On clicking on authorize you will get a prompt like this , please go ahead and select your account you will get a security prompt from google , please allow that.
Click on the blue box as shown below “Exchange authorization code for tokens”
You will get a response something like this where I have highlighted the token that we will need. Do the above step every time to get a new token. You can use refresh token to refresh you access token but I will discuss that in my next blog where I will discuss about permanent oauth2 flow in google apis.
for better readability the access token mentioned here looks something like this.
Please note all these keys will be different in your case , using these wont work as you have follow these steps in your own Gmail account 😀
Next go to and click on try this api , click on the square as shown in circle
q box is where your search query goes , lets say i want to list all emails (for deletion finally) from firstname.lastname@example.org the q box will be filled as from:email@example.com
copy the http code by clicking on the copy icon which is shown as encircled
You copied code should be seen something like this
GET https://gmail.googleapis.com/gmail/v1/users/[USERID]/messages?q=from%3Aabcd%40gmail.com&key=[YOUR_API_KEY] HTTP/1.1
Authorization: Bearer [YOUR_ACCESS_TOKEN]
Your url is the text between GET and HTTP/1.1
Now remove &key=[YOUR_API_KEY] and replace [USERID] as me in the url to get final url as
So now you have token and url, you need these 2 parameter to run the as hosted in github.
If everything goes fine your emails should be deleted with a massage “No more mails”
For any queries you can reach out to me at