Saturday, February 20, 2016

Cleaning Out Old Mail in Gmail Automatically

Mike Cleckner
Software Engineering | Project Management • Software Development • Quality Assurance
linkedin.com/in/mcleckner



Coffee Mug
This morning, with my cup of coffee in hand, I sat down at my computer and started to go through my email. I finished clearing out my inbox using my GTD strategy and then decided to clean out old mail that I don't care about.

I've been using filters in Gmail to automatically label and move my email out of my inbox.

That certainly cuts down on the amount of mail I have to deal with my inbox. I'll periodically read the email I've filter, but sometimes I don't really care about them and it builds up. I realize we've got oodles of "free" email storage with Gmail, but it really bugs me to see all those unread messages piling up.

I've been relying on my PC based mail application to archive certain mail or manually entering a search criteria within Gmail. I've been using Thunderbird for my Email Client, but I'm finding more and more that I just prefer to use the Gmail web interface. Thunderbird seems to be bloating and it's slow to run on my PC (Ok, so my PC is 5 years old, but it works. I'm not upgrading just for a better Email experience).

To trash these old emails, I use a search query finding all the mail for a certain label older than so many days and not part of my GTD strategy.

label:Newsletters older_than:10d -label:_Keep -label:!NextAction -label:!Waiting -label:!Delegated -label:!Someday -label:!Scheduled

I never figured out a way to automatically clear out old junk from within Gmail. Filters don't work, because they are for mail as it arrives. A few choice Google searches and I came across this post "Set Gmail To Auto-Delete Emails Older Than A Set Number Days" from 2013, which inspired me to revive my coding skills and have at it.

I used the basic concept from that post and combined it with a Hash Table implementation to create my own automatic clean up Google Script for those "I might read sometimes" emails; each label having its own retention policy. I have scheduled it to run once a day.

The Solution

My solution differs from the original post's implementation:

Thumbs Up
  1. I am not using a Gmail Filter to tag email as "delete me," because I already have filters set up to label my emails. 
  2. I am not using a blanket age criteria. Based on the label, I can have different retention ages (this is where the Hash Table implementation comes into play).
  3. I am using the GmailApp.search() to find the emails to clean up instead of GmailApp.getUserLableByName().

First, I created a new Google Scripts project and imported the Hash Table implementation and called that script file Class-HashTable.gs.

Second, I created a new script file in my project, cleanUp.gs, and implemented my version of the cleanUp() function.

function cleanUp() {

  var MailCleanHash = new HashTable( {Weather: 7, Newsletters: 10} );

  for (var i = 0, keys = MailCleanHash.keys(), len = keys.length; i < len; i++) {

    // I want to exclude anything that I have labeled "_Keep" or is Starred
    var excludeStr = "-label:_Keep -label:!@Next_Action -label:!Waiting -label:!Delegated -label:!Someday -label:!Scheduled";
 
    var searchStr = "label:" + keys[i] + " " + excludeStr + " -is:starred older_than:" + MailCleanHash.getItem(keys[i]) +"d";
 
    var threads = GmailApp.search(searchStr);
    for (var t = 0; t < threads.length; t++) {
      threads[t].moveToTrash();
    }
  }
}


Third, I set my trigger for the script to run once a day.

Done!

Now my email will automatically be cleaned up without my intervention.