Automatically delete old emails from Gmail

I am fastidious about keeping my systems tidy, but until recently I had been unable to solve one particular issue: removing old messages from Google’s email platform, Gmail.

I have been a heavy Gmail user for decades. Because of that, the archive is as useful as the inbox to me. It is often the first place I go to answer everyday questions, such as “When did we go on holiday to…?” or “When did I buy that pair of jeans?” As almost everything in my personal life goes through that archive, the answer is normally there, in receipts, in booking confirmations, or similar.

Yet there is also other stuff there. Things I don’t need. The bulk of this is notification or update email that is useful at the time, but useless months after the event. For example, I have an email alert for new property for sale in my area. That’s very useful when I receive it, but it’s not so useful a few months later, when that property has been sold. Now I can use Gmail’s “Rules” feature to tag these messages and auto-archive them, but I was frustrated that I couldn’t “expire” them after a few months without manually filtering them and hitting delete.

However, with some online suggestions and some coding help from AI, I have come up with a solution that stacks two great Google features:

  1. The awesome search and filter labels in Gmail, particularly being able to search for messages older_than:1m (older than one month).
  2. Google’s Apps Script, which lets you script with access to your Google Account and the apps and data therein.

With the help of these tools, some internet inspiration and some AI coding, I created a new “project” in Apps Script called Gmail Cleaner, and replaced the boilerplate code in the main file (code.gs) with the following:

function GmailCleaner() {

  // Define an array of criteria

  var criteriaList = [
    "label:property older_than:2m",
    "label:career older_than:1m from:\"LinkedIn Job Alerts\""
  ];
  
  // Process each criteria using a helper function

  criteriaList.forEach(processCriteria);
}

function processCriteria(criteria) {
  var conversations = GmailApp.search(criteria);
  conversations.forEach(function(thread) {
    thread.moveToTrash();
  });
}

In this code you should be able to see that I am deleting email that meets any of the following criteria:

  • Emails that I have auto-tagged “property” (and which already skip the inbox) and which are more than two months old
  • Emails that I have auto-tagged “career”, which are older than one month, and which come from “LinkedIn Job Alerts”.

You can add more criteria, just be sure to put a comma between them.

Note that I have escaped the speech marks above – you may need to do this too.

Once you’ve got the code right for you (and I suggest you test the filters in Gmail search first, before adding them to the script) you need to save the file, and set it up to “deploy”. To do this you can set a “time trigger”. Absurdly, you need to select the option to deploy from the “Head”. I don’t know what that means, but I don’t care, because it works.

When I tried to run this the first time, I needed to authorise Apps Script to access my Gmail account, and, amusingly, my Google browser did issue a security alert about letting this Google service access my other Google service. However, once I’d jumped that hurdle, the script has been running perfectly for weeks.

I now have this running every 6 hours, making my Gmail archive a lighter, cleaner, happier place.


Comments

Leave a Reply

Good morning