Skip to content

Instantly share code, notes, and snippets.

@MSakamaki
Forked from gabmontes/gmailCleanup.js
Created April 2, 2022 04:23
Show Gist options
  • Save MSakamaki/d975993febc097ba9fb8d6b28ce3558d to your computer and use it in GitHub Desktop.
Save MSakamaki/d975993febc097ba9fb8d6b28ce3558d to your computer and use it in GitHub Desktop.
Google Apps script to cleanup messages from GMail.
// Inspired in http://www.johneday.com/422/time-based-gmail-filters-with-google-apps-script
// Deletes old marked conversations
function cleanUp() {
var delayDays = 5; // # of days before messages are moved to trash
var label = "Delete me"; // label to identify the messages
var maxDate = new Date(Date.now() - delayDays * 24 * 60 * 60 * 1000);
var userLabel = GmailApp.getUserLabelByName(label);
if (!userLabel) {
return;
}
var threads = userLabel.getThreads();
threads.forEach(function (thread) {
if (thread.getLastMessageDate() < maxDate) {
thread.moveToTrash();
}
});
}
// Deletes old conversations in selected categories
function batchDeletePromotions() {
var categories = ["social", "promotions", "forums", "updates"];
categories.forEach(function (category) {
GmailApp.moveThreadsToTrash(GmailApp.search('label:inbox category:' + category + ' older_than:2d'));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment