Tag Archives: bulk

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.

Deleting old emails in Gmail

Gmail is great, with nearly 8gb of storage there really is no need to delete old emails, instead just archive them and then when you need to find an email from 4 years ago you can find it via the powerful search engine.

However I have a collection of emails which are auto filtered from my webserver letting me know that the firewall has blocked access to an IP or that I’ve logged in with the root password. Whilst useful at the time, not really need for more than a month, let alone years. You might have the same with a discussion board, or freecycle.

So the quick way to delete them all:

In the search box enter label:webserver older:2011/05/12
Once loaded you should be able to click on the select all button (although it’s no longer labelled).
Just above the first email you should now see “All 20 conversations on this page are selected. Select all conversations that match this search”. Click on the link.
You can now click on the Trash/Delete button.
All the old emails have now been trashed.

For the search field the date is in YEAR/MONTH/DAY format, instead of label: you can use from: so from:[email protected] older:2010/06/12 for all emails from Lincoln Freecycle older than a year.