Skip to content

Instantly share code, notes, and snippets.

@HeroCC
Created March 20, 2022 21:28
Show Gist options
  • Save HeroCC/907f063e08cedd7deed23143cb0508d2 to your computer and use it in GitHub Desktop.
Save HeroCC/907f063e08cedd7deed23143cb0508d2 to your computer and use it in GitHub Desktop.
Trash & delete old promotions from Gmail inbox
// Go to https://script.google.com/home
// Create a new script, pase all of this, and click run
// The first run will ask you for permissions, and it may time-out before trashing all the emails, so run it again as necessary
// Optionally, create a time based trigger to run the script periodically (when first clearing my email I had it run every ~15 mins, but now once a day)
function purgeGmail() {
// Paste this query in your gmail search bar BEFORE running the script, and make sure nothing you need is matched
const search = "category:promotions older_than:1y -is:important -is:starred -has:attachment";
Logger.log("Removing messages matching: " + search);
length = 1;
while(length > 0) {
const threads = GmailApp.search(search, 0, 500);
var c = 0;
for (var i=0; i<threads.length; i++) {
const thread = threads[i];
Logger.log("Moving to trash: " + thread.getFirstMessageSubject());
thread.moveToTrash();
c++;
}
Logger.log("Deleted a batch of " + c + " messages.");
length = threads.length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment