Gmail is wonderful. Whether you are using the free version or using a Google Apps account, it’s a hard-to-beat service. Since it’s launch it has revolutionized the amount of free storage available for emails and it’s still growing every day.

However, it does lack some fairly simple features when you are used to full-blown desktop clients such as Outlook, Evolution or Thunderbird. The one I used to miss the most is the option to automatically delete emails which are older than a given date. I use Gmail for a whole lot more than just my private emails. A couple of months ago I started using IFTTT as my RSS reader. When one of the blogs I subscripted to has a new message, it sends me an email with the blog post content. This means that everything I want to read exists right there in my inbox.

Unfortunately, this means my inbox is growing each day. And while I could technically remove each item after I’ve read it, I sometimes want to re-read it, or save it for later. It’s far easier for me to simply mark it as done and go on.

When I used Outlook, I would set up auto-archive functions on my inbox folders which would automatically delete all mails older than a given time. Out of the box, Gmail doesn’t support such a thing.

Luckily, Google has provided us with something amazing: Google Apps Script. A scripting service using a JavaScript based language which allows us to interact programmatically with most of their services. Granted, it’s not something useable for most non-tech-savvy users, but it allows us to achieve our goal: remove old mails by label.

Scripting the deletion process

When you have logged in, you will be greeted with an empty script:

function myFunction() {

}

In order to automate or process, we will first have to write some code to fetch the corresponding emails and remove them. Later on, we can use a scheduler to automate the function calls. We will be using the Gmail Service to perform all our operations.

There’s no need for the myFunction function, so we can simply remove it.

First, there are some helper methods. All of them can simply be placed in the same file, one after the other.

// Create a Date object representing a date in the past using the number of days
// specified.
function getDate(days) {
  var maxDate = new Date();

  maxDate.setDate(maxDate.getDate() - days);

  return maxData;
}

// Move the matching threads to the Trash.
function moveThreadsToTrash(threads, maxDate) {
  for (var i = 0; i < threads.length; i++) {
    var thread = threads[i];

    if (thread.getLastMessageDate() < maxDate) {
      thread.moveToTrash();
    }
  }
}

// Clean a specific label
function cleanByLabel(labelName, days) {
  var label = GmailApp.getUserLabelByName(labelName);
  var threads = label.getThreads();
  var maxDate = getDate(days);

  moveThreadsToTrash(threads, maxDate);
}

Note: If you are using Google’s new Inbox, you can search on bundles using the search function.

Once those are in place, we can create specific functions per label we want to remove. In my case I would like to remove all the emails from the News label which are older than 2 days, and all mails from the Blog label which are older than 7 days.

function cleanBlogs() {
  cleanByLabel("Blog", 7);
}

function cleanNews() {
  cleanByLabel("News", 2);
}

Automating

Once the script is in place and saved, we can automate the cleanBlogs and cleanNews function calls. Click on the Resources tab above, and select Current project's triggers. Enter a project name and you will be present with a screen allowing you to set up some trigger schedules.

Add triggers

Select the function you want executed and select the schedule.

Enter schedule

Save your changes, et voila.