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.
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.
Lets start
Before you start you should know how to search email using search
. 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
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
, 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.
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.
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.1