Skip to content

Instantly share code, notes, and snippets.

@kensaggy
Last active December 20, 2015 10:39
Show Gist options
  • Save kensaggy/6117662 to your computer and use it in GitHub Desktop.
Save kensaggy/6117662 to your computer and use it in GitHub Desktop.
A Google App Script to delete a large quantity or threads by a label name
function Rubi() {
//Set the below parameters to your needs
var labelName = "test_label"; //edit this to your liking
var batchSize = 1; //should probably set to something high, but not too high. (300? 1000? or whatever)
// get the label for given name
var label = GmailApp.getUserLabelByName(labelName);
var from = 0;
do {
//Get new threads by the batchSize
var threads = label.getThreads(from, batchSize);
Logger.log("Now deleting messages " + from + " to " + (from+batchSize));
//Loop over threads in this batch and move them to trash
for (var i = 0; i < threads.length; i++) {
Logger.log("Delete thread subject: " + threads[i].getFirstMessageSubject());
//TODO: uncomment the below line to actually delete
//threads[i].moveToTrash();
} //for loop
//Increase from variable for next batch
from += batchSize;
} while (threads.length > 0); //Do as long as the label isn't empty
Logger.log("Done. No more messages under label: " + labelName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment