Skip to content

Instantly share code, notes, and snippets.

@balupton
Created March 27, 2014 05:29
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save balupton/9800939 to your computer and use it in GitHub Desktop.
Save balupton/9800939 to your computer and use it in GitHub Desktop.
Remove script for Gmail that delets all email threads/messages that match search for when Gmail can't do it itself

Remove script for Gmail

function Intialize() {
  return;
}

function Install() {
  ScriptApp.newTrigger("purgeGmail")
           .timeBased().everyMinutes(10).create();

}

function Uninstall() {
  
  var triggers = ScriptApp.getScriptTriggers();
  for (var i=0; i<triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }
  
}

function purgeGmail() {
  var search = "to:log@docpad.org";
  
  var threads = GmailApp.search(search, 0, 100);
  
  for (var i=0; i<threads.length; i++) {
    var thread = threads[i];
    thread.moveToTrash();
  }
}
  1. Go to https://script.google.com and create a new script
  2. Copy the above to the script input
  3. Click the save icon
  4. Click the "Run" menu
  5. Click the "Install" submenu entry
  6. Wait a few days, it will delete 100 messages each 10 minutes
@abcarroll
Copy link

You can select up to 500 at a time.

This is the same, but as an infinite loop.

function purgeGmail() {
  Logger.log("Beginning removal...");
  var search = "label:web-app-suspicious";
  
  length = 1;
  
  while(length > 0) { 
    var threads = GmailApp.search(search, 0, 500);
    
    var c = 0;
    for (var i=0; i<threads.length; i++) {
      var thread = threads[i];
      thread.moveToTrash();
      c++;
    }
    
    Logger.log("Deleted a batch of " + c + " messages.");
    length = threads.length;
  }
}

@vishuenc
Copy link

vishuenc commented Jun 8, 2020

If i want to delete all messages which are from "abc@mail.com" then how do we achieve with this script? Please advise.

@schspa
Copy link

schspa commented Feb 5, 2021

If i want to delete all messages which are from "abc@mail.com" then how do we achieve with this script? Please advise.

just change the search partition:
var search = "to:log@docpad.org";
to
var search = "from:log@docpad.org";

@schspa
Copy link

schspa commented Feb 5, 2021

There is an API to remove all threads to trash. but at most 100 thread.
GmailApp.moveThreadsToTrash(threads)

@realugbun
Copy link

realugbun commented Aug 30, 2021

I wanted a script to remove emails that are older than a certain time. I get notifications from Jira and other services which I want to receive but I don't want to keep them. I've modified the function to allow an array of search terms. I didn't see anything in the documentation about moveThreadsToTrash only supporting 100 messages at a time but I could be wrong. The docs say the call will fail if there are "too many threads." When you run the script it comes back with an error stating there is a max of 100 threads.

function purgeGmail() {
  // Array of search queries
  const queries = ["from:example1@example.com older_than:2d","from:example2@test.com older_than:7d"];
  
  for (let i=0; i<queries.length; i++) {
    GmailApp.moveThreadsToTrash(GmailApp.search(queries[i]));
  }
}

@lgs003
Copy link

lgs003 commented Jan 29, 2023

hi, can you help me
when i ran this script ,timeout happened
return the error Exceeded maximum execution time

@anqin
Copy link

anqin commented Feb 17, 2023

After I run the "install" and wait for above 10 mins, it seems nothing happen. The purgeGmail() had not been called yet.

@jacevedo1450
Copy link

Thank you for this script. Is there a way to delete messages based on the subject? Unfortunately, in my situation, we have an all-employee group and I don't want it to find all emails to that group and delete them. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment