How to bulk delete e-mails in Gmail

I found myself running out of space on an old Gmail account of mine that I had for over 10 years.  There was over 800,000 e-mails in it and I didn’t need anything that was older than 5 years.  The problem was that Gmail has no simple way to bulk delete old e-mails.  After some searching I believe that I have the best option thanks to Bruce Bates and Martin Cammi.  Below is the solution that I used in order to delete tens of thousands of old Gmail e-mails.

Steps to remove tons of old Gmail e-mails

  • Head to Google Scripts and create a new script using the following code (this code removes all e-mails older than 5 years)
function batchDeleteEmails() {
    var batchSize = 100; // Process up to 100 threads at once
    var threads = GmailApp.search('label:inbox older_than:5y'); //removes e-mails older than 5 years 
    for (j = 0; j < threads.length; j+=batchSize) {
        GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize));
    }
}
  • Click “Run” –> “Run Function” –> “batchDeleteEmails”  This will then prompt you with some permission requests for your script to access your signed in gmail account.  You will need to give the script access before it can run.  Make sure you run this at least once and then check your bin folder to ensure that e-mails are being deleted.  Once you’re sure that they’re being deleted you can move to step 3 and 4.
  • Sadly this script will only run for a certain time period.  To increase the amount of e-mails that will be deleted we will tell Google to run your script every hour on the hour by doing the following:
    • Click on the “My Projects” icon and on your script click on the “three vertical dots” and then select Triggers
    • Click “Add Trigger” and make sure that the “Select hour interval” is set to every hour

The above two steps will ensure that e-mails older than 5 years are removed.  This script will run every hour.

Some notes

  • You can tailor emails to be deleted based on other parameters by updating the code label:inbox older_than:5y in line three of the above code to be:
    • label:inbox older_than:1d –> will delete any e-mail in your inbox older than 1 day
    • has:attachment larger:3M –> will delete any e-mail in your inbox with an attachment larger than 3 Megabytes