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.
Scripting the deletion process
When you have logged in, you will be greeted with an empty script:
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.
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.
Once the script is in place and saved, we can automate the
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.
Select the function you want executed and select the schedule.
Save your changes, et voila.