Automatically deleting old Gmail email
Like many of you I’m a gmail hoarder. I never deleting anything, just “archive” everything. I “might” need it later, or “I’ll get to it when I have time”. If we get really honest with ourselves, we never will actually get to it, and because we have this buffer, this procrastination opportunity, we grab it. We use words like “but I may need proof of X”, or “I could need to reference this”, or “I don’t really want to put this person in my contacts so I’ll just save the email”.
Personally I have made a choice to force myself to be more engaged in my email going forward. I have set a Google Script to run through all of my email every day and if it is older than 90 days, and doesn’t have the specail label “keep” on it, it’s gone. I obviously didn’t care enough about whatever was going on in that email to take the time to respond to it.
Whatever your reason for finding this post, here is how I got it done:
At first I started with just searching for a way to make a simple filter to do this. There are plenty of blog posts that show you this is possible, but it turns out that at some point Gmail switched to only applying filters to incoming messages (makes sense, lots less overhead, no need to load EVERYONEs email every hour). This makes deleting messages older than X impossible with simple filters however. I did however learn of a search query term you can use to find messages/threads older than X. Imaginatively called
older_than:. So a search in Gmail for
older_than:90d resulted in exactly the emails I wanted to find.
Using the API reference here: https://developers.google.com/apps-script/reference/gmail/ I was able to cobble together the following in about 20 minutes:
Now, that script leaves a lot to be desired. It doesn’t have any case where a label like “save” or “keep” might come into play, it just hauls off and deletes everything old. But before we start improving lets show you how it looks when you’re actually doing it (I’ll comment out the “moveThreadToTrash in the screenshots)
Go to https://script.google.com/ and you’ll be presented with a new project if you haven’t been here before:
Then, copy and past the code in and save it. It’ll ask you to name the project. I just named it DeleteOldEmail. Feel free to name it anything you wish. It is what is going to show up in your Google Security “Approve Apps” list.
The first time I ran this I commented out the lines for deleting and added in a “Logger” line just so I knew what would be deleted:
Then run it, the first time you run it, it will ask for permissions:
If you try this out and decide you don’t like it you can revoke the permissions here: https://myaccount.google.com/permissions
If you went with the logging you’ll see something like this if you go to View->Logs:
Last step is to set this up so it automatically runs, I mean that’s the whole point. For that, we go to Edit->Current Project’s Triggers. And we are greeted with the following (once we click to add a new trigger):
I chose to set up mine to run every 12 hours, but you can set it to run once a day, once a week, whatever.
Again, this script leaves a lot to be desired, You can totally mess with the query search string any way you wish. I’ve created a Github repository that I’ll put improvements on the script: https://github.com/mubix/GScriptOldEmal
Have fun with it. The two improvments I’ll be adding are the label exclusion for “keep” and notifications (a list of email subjects deleted)
Thanks for your time.