// 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')); }); }