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
@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