Skip to content

Instantly share code, notes, and snippets.

@dmuth
Last active February 17, 2016 20:00
Show Gist options
  • Save dmuth/03c474b4697d47ca6678 to your computer and use it in GitHub Desktop.
Save dmuth/03c474b4697d47ca6678 to your computer and use it in GitHub Desktop.
Delete threads from your Gmail mailbox in bulk Raw
//
// Paste this function into the window at http://script.google.com/ and
// then click the triangle button in the menu bar to run it.
//
// After the script is finished running (which can take a minute or longer!),
// go to View -> Logs to view the logs of the script execution.
//
// Based off of a script I found at https://productforums.google.com/forum/#!topic/gmail/YeQVDuPIQzA
//
function batchDelete() {
//
// How many main loops?
//
var numLoops = 4;
//
// How many threads to process at once?
//
// The total number of emails deleted will be numLoops * batchSize
//
var batchSize = 100
//
// Total number of messages deleted?
//
var total = 0;
//
// Our starting time
//
var startTime = new Date().getTime() / 1000;
for (i=0; i<numLoops; i++) {
Logger.log("Starting search...");
var threads = GmailApp.search("subject:Nightly Cron Job");
Logger.log("Search done!");
for (j = 0; j < threads.length; j += batchSize) {
total += batchSize;
Logger.log("Loop %s: Deleting batch starting at %s", i, j);
GmailApp.moveThreadsToTrash(threads.slice(j, j + batchSize) );
//
// Calculate seconds elapsed and average number of threads deleted per second.
//
var time = new Date().getTime() / 1000;
var diff = time - startTime;
var rate = total / diff;
Logger.log("Deleted total of %s threads (%s threads/sec)", total, rate);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment