Archiving Gmail

| May 13th, 2014

I’m terrible at deleting/archiving old emails in Google Apps, and so after getting fed up with my inbox being storage for the last year or 2′s emails I decided to see if there was a way to automatically archive emails after a certain date.  I found a few pages out there and here’s the summary.

There is no way to do this with regular gmail filters, but I stumbled across this little thing called Google Scripts, which lets you do a whole lot of awesome stuff with the google platform.

For you programmers out there it uses a javascript cloud scripting language, similar to the cloud scripting in Parse (Which you should check out if you haven’t already).

So down to business:

  • Open up http://script.google.com
  • Select “Create script for – GMail”
  • Delete everything in the code box and paste the following in:
function autoArchive() {  
    var emails = GmailApp.search('label:inbox is:read older_than:14d');
    for (var i = 0; i < emails.length; i++) {
        emails[i].moveToArchive();
    }
}
  • Change the number after older_than to the amount of days you would like to archive to.
  • Name the script something nice (I called it “Gmail Auto-Archive”).
  • Select “Current Project Triggers” from the “Resources” drop-down.
  • Click the add trigger link and select how often you would like it to run. (I chose to run it every night at midnight, but it’s up to you.)
  • Click save, this will bring up a pop-over asking for permissions to access your Gmail inbox, click OK and you are good to go!

Extra Stuffs:

– If you would like the script to also archive unread emails, just remove the “is:read” section from the search clause.

– If you want to immediately run the script to clean out your inbox before the next scheduled time (If you’re impatient like me), on the code page click the play button in the toolbar, it will run the code straight away.

NOTE: After a bit of testing it seems google scripts aren’t allowed to run more than 5 minutes, so if you are attempting to do this the first time and you have a lot of emails in your inbox (Over 500 or so), the script will need to be restarted every 5 minutes until all the emails are archived. This should only be a problem the first run if you are running the script fairly often. (I had to run the script 5 times to archive my initial 2000 email inbox).

– You can do a heap more awesome things with google scripting, for more info go here: https://developers.google.com/apps-script/ theres a heap of tutorials and information.

Hope this helps anyone out there!

Comments are closed.