I donโt know about you, but I receive tons of emails each day in my Gmail inbox. Some are my fault; I had signed up for a newsletter on a whim because it was something I was actually interested in (like sales at Michaelโs or DSW), and others might just be a result of third-party companies selling your data for profit and you end up on some list thatโs kind of related to a thing you bought once on eBay three years ago.
Since I’m a developer and this was a pressure point for me with technology, there had to be a way for me to fix this problem with code! I managed to do so with a Google Script and I am sharing it with the world!
I used to be vigilant in sorting my email. Iโd go through everything and delete promotions that were no longer in date, archive reports on accounts from weeks ago, and sort my client emails in the inbox into folders. But then it started taking longer and longer to sort my Gmail. There was just too much! So I started to unsubscribe or update my preferences to be less frequent, but I still had thousands of emails to delete or archive, so what to do?
First, I looked at the Spam and Trash folder since they already had this feature. I had hoped to find a checkbox or something in my settings to simply enable it. Unfortunately, this was not the case. Next, I searched Google. While I am a developer, part of my mantra includes not reinventing the wheel. Surely Iโm not the first person to want this, so maybe someone already created something to help!
I came upon this Gmail Auto Purge tool by Digital Inspiration, written in Google Scripts. It is a very simple tool that simply takes a Gmail label and the number of days and then it just deletes emails based on those parameters. However, it didnโt encompass all of my needs. For starters, I need to also automatically archive emails in the Updates category because I donโt want to delete them forever (like digital receipts for tax purposes), and secondly, it only used the โlabelโ parameter in the search and I needed to use โcategoryโ. Thankfully, Digital Inspiration posted their source code for the script, so it gave me a starting point. Here is the criteria I wanted to be able to change about their script:
- Ability to choose a โDo Thisโ action, even if itโs just a boolean between deleting and archiving. Ideally, Iโd like to have the same options as I do when creating a filter.
- Ability to use any search query, specifically โcategory:promotionsโ, โcategory:updatesโ, and โcategory:socialโ.
- Change how often it runs from once a day to every week or even once a month. As a developer, I feel like I need to consider processing power, and I donโt particularly care if I have an email in my Promotions category that is exactly 30 days and 5 hours old. It can wait until the script runs again every 3 days or so to delete that one.
So with that, I wrote my first Google Script to solve all my problems. Yay, technology!
This article was originally published on July 20, 2017, on the Flying Cork blog.
