Gmail was launched on April 1, 2004 and I've had an account from almost day one! The amount of email that has been stored in there for absolutely free is, frankly, pretty astonishing.
But, slowly over time, it has started to fill up. Even though I still have over half of my free space available to me, I figured it was about time to start planning before I got myself into trouble! I needed to clean my email!
If you are planning to follow some of the instructions/ideas here, be aware that there is a possibility that you could lose a valuable email. If you are concerned about this happening, then I suggest that you use Google Takeout to backup your email before doing anything here. Then, you will be able to recover that precious email you accidentally deleted.
Automating the Clean
I knew right from the start that going through every single email that I had received and deleting them one at a time just was not going to be an option. Even if I were able to assess each email and then delete it accordingly in 5 seconds, this would still take several days working around the clock.
There had to be a better way...
I immediately thought about automating the process using n8n. This way, I could configure the parameters that I wanted and let n8n do all the work!
My first try was a pretty straight forward attempt:
I simply used two Gmail nodes. The first retrieved all of the Gmail messages that were not in chats that had the word unsubscribe in it but not the words license, key or password.
This was accomplished by setting the following parameters in the node:
The Query parameter reads as follows:
-in:chats unsubscribe -license -key -password
This basically tells the node to ignore chats and look for the term unsubscribe (which will presumably be mailing lists and newsletters) but ignore emails if they have the term license, key or password as there is a good chance that these will contain keys for software that I have purchased.
This is the same search format that is used in the Gmail Search bar:
This is very useful for testing to see if you are getting the right search results.
It was important to set the Format to IDs as this returns the least amount of information possible to n8n. Otherwise, the loading process will take too long and consume too much memory.
The second node simply tool all of the IDs from the first node and then deleted the email with that ID.
I then ran the workflow and waited...
Eventually, I gave up waiting and went about my day. When I came back a while later, the workflow had crashed when the Gmail delete node received a 500 error from the API. This was basically telling me that API encountered an error and crashed.
The other issue I was having was that I couldn't really get a good idea on the progress of the process.
So I made some changes...
I figured I'd try to resolve both of these issues with one change.
I added a SplitInBatches node between the two Gmail nodes and configure it to process 100 emails at a time. This way, it will give the API a short rest between batches and I will see the batch count increment on the node with every batch that is completed.
The updated workflow looked like this:
This worked a lot better and the workflow finished successfully! You can copy this workflow below if you wish to use it:
Refining the Cleaner
The cleaner worked quite well, reducing my total storage by 1.24 GB (from 7.07 to 5.83 GB shared across Google Drive, Gmail and Google Photos)! That's a lot of email!
But, I believe that it can be even better by refining the Query parameter in the Gmail node. Here are some other useful values that you can set for the Query parameter:
- Specific email address -
- Promotions category -
- Password resets -
"password reset" OR "reset password"
- Updates before a specific date -
- Muted emails -
- Emails older than a month -
For more search operators, you can find a full list in the Gmail Help.
Scheduling and Alerting
Now that I have figured out how to clean out my inbox, it's important to keep it that way by running the workflow on a regular basis.
To do this, I added a Cron node at the beginning of the workflow and configured it to run each day at midnight.
It is also important to be alerted when a change has occurred on one of your systems so I added two more nodes at the end of the workflow:
- Calculate the number of emails deleted.
- Send a message to Discord indicating the number of emails deleted.
My final workflow looks like this:
When the workflow runs, it sends a message to Discord that looks something like this:
Using an n8n workflow, it is rather trivial to search and remove unwanted emails from your Gmail account so that it is easier to manage and maintain.