Guide to deleting emails using google/gmail apis

Jyotiska Bhattacharjee
5 min readJan 27, 2021

Hoping at the end of the blog you should be able to delete large number of emails using google apis, you should not need any technical or coding background if you follow the steps below in exact manner.

Context

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. Following 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.

Let’s begin

Before you start you should know how to search email using search query. For example if you want to list all emails from abcd@gmail.com then you must type from:abcd@gmail.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 queries , the script 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 OAuth 2.0 Playground 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 script 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 criteria , 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 OAuth 2.0 Playground and select the scope as https://mail.google.com 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.

ya29.a0AfH6SMCa0aRk0P_4U9se7l4FLS-nEssII_Z77uRZuaf2ZR_naBQpyrx-H9KMCpHQppvGoGWp9F6cAl2U_97qClMGoTIZUkyue-QvgplUtzVDTvUqzrDAOSDBQ3l-5OBqRKHZAIyfat0fbML1HrY5uOgL1zYfLizXDb6hZJJWvg4

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 https://developers.google.com/gmail/api/reference/rest/v1/users.messages/list 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 abcd@gmail.com the q box will be filled as from:abcd@gmail.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.1Authorization: Bearer [YOUR_ACCESS_TOKEN]Accept: application/json

Your url is the text between GET and HTTP/1.1

“https://gmail.googleapis.com/gmail/v1/users/[USERID]/messages?q=from%3Aabcd%40gmail.com&key=[YOUR_API_KEY]”

Now remove &key=[YOUR_API_KEY] and replace [USERID] as me in the url to get final url as

url='https://gmail.googleapis.com/gmail/v1/users/me/messages?q=from%3Aabcd%40gmail.com'

So now you have token and url, you need these 2 parameter to run the script 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 jyotiska.bhattacharjee@gmail.com

--

--