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