Automatically deleting old emails in Gmail

As a follow up to my post Deleting old emails in Gmail 3 years ago, I’ve now found a way to automatically delete old emails from Gmail.
This trick uses the rather unknown Google Scripts, a Javascript engine which runs on Googles servers.

  • Pop to Gmail and test some searches, eg label:webserver older_than:1m this will find all emails labelled webserver and are older than 1 month (full list of search options here.
  • Once you are happy that your search result has found the emails you don’t want, go to script.google.com.
  • Select Create script for GMail
  • Delete all the example code
  • Enter this code
    function deleteOldEmails() {
    var threads = GmailApp.search('label:webserver older_than:1m');
    GmailApp.moveThreadsToTrash(threads);
    }

    ensuring you replace label:webserver older_than:1m with your search phrase
  • Save you project
  • Press run
  • Once the notification at the top has gone, check back in Gmail to confirm your emails have been deleted.
  • To automate, click on the Clock icon, Add a new trigger. Select your function, you want it Time-driven, and for me I’ve selected Month timer, 1, Midnight to 1:00am

The result will be on the first of every month Gmail deletes all emails older than 1 month from my webserver folder.

Leave a Reply

Your email address will not be published. Required fields are marked *

Human test: Enter Ryan backwards