|
// The name of the Gmail Label that is to be checked for purging? |
|
var LABELS_TO_DELETE = [ |
|
"notifications-github", |
|
"notifications-trello", |
|
"notifications-hipchat" |
|
]; |
|
|
|
var TRIGGER_NAME = "dailyDeleteGmail"; |
|
|
|
// Purge messages in the above label automatically after how many days? |
|
var DELETE_AFTER_DAYS = "4"; |
|
|
|
var TIMEZONE = "AEST"; |
|
|
|
// Maximum number of threads to process per run |
|
var PAGE_SIZE = 150; |
|
|
|
// If number of threads exceeds page size, resume job after X mins (max execution time is 6 mins) |
|
var RESUME_FREQUENCY = 10; |
|
|
|
/* |
|
|
|
IMPLEMENTATION |
|
|
|
*/ |
|
function Intialize() { |
|
return; |
|
} |
|
|
|
function Install() { |
|
|
|
// First run 2 mins after install |
|
ScriptApp.newTrigger(TRIGGER_NAME) |
|
.timeBased() |
|
.at(new Date((new Date()).getTime() + 1000*60*2)) |
|
.create(); |
|
|
|
// Run daily there after |
|
ScriptApp.newTrigger(TRIGGER_NAME) |
|
.timeBased().everyDays(1).create(); |
|
|
|
} |
|
|
|
function Uninstall() { |
|
|
|
var triggers = ScriptApp.getProjectTriggers(); |
|
for (var i=0; i<triggers.length; i++) { |
|
ScriptApp.deleteTrigger(triggers[i]); |
|
} |
|
|
|
} |
|
|
|
function dailyDeleteGmail() { |
|
|
|
var age = new Date(); |
|
age.setDate(age.getDate() - DELETE_AFTER_DAYS); |
|
|
|
var purge = Utilities.formatDate(age, TIMEZONE, "yyyy-MM-dd"); |
|
var search = "(label:" + LABELS_TO_DELETE.join(" OR label:") + ") before:" + purge; |
|
Logger.log("PURGE: " + purge); |
|
Logger.log("SEARCH: " + search); |
|
|
|
try { |
|
|
|
var threads = GmailApp.search(search, 0, PAGE_SIZE); |
|
|
|
// Resume again in 10 minutes |
|
if (threads.length == PAGE_SIZE) { |
|
Logger.log("Scheduling follow up job..."); |
|
ScriptApp.newTrigger(TRIGGER_NAME) |
|
.timeBased() |
|
.at(new Date((new Date()).getTime() + 1000*60*RESUME_FREQUENCY)) |
|
.create(); |
|
} |
|
|
|
// Move threads/messages which meet age criteria to trash |
|
Logger.log("Processing " + threads.length + " threads..."); |
|
for (var i=0; i<threads.length; i++) { |
|
var thread = threads[i]; |
|
|
|
if (thread.getLastMessageDate() < age) { |
|
thread.moveToTrash(); |
|
} else { |
|
var messages = GmailApp.getMessagesForThread(threads[i]); |
|
for (var j=0; j<messages.length; j++) { |
|
var email = messages[j]; |
|
if (email.getDate() < age) { |
|
email.moveToTrash(); |
|
} |
|
} |
|
} |
|
} |
|
|
|
} catch (e) {} |
|
|
|
} |
Thanking a lot for this amazing code. I am using it to get rid of hundreds of emails on a regular basis AUTOMATICALLY! This script has saved a lot of effort and increased productivity. I wrote a post about it here: https://www.linkedin.com/posts/theparin_gmail-delete-old-emails-automatically-activity-6782625622943498241-Et86
Initially, 4 months ago, I had a problem deleting emails from multiple labels when I was putting the names of multiple labels in the script and it didn't delete any of them at all. But, Just now, at this very moment, I found a 10-year old answer on https://webapps.stackexchange.com/questions/10581/filtering-based-on-multiple-labels-in-gmail , saying that we need to add '-' sign if the label names have spaces in them. And now, it is working properly as it should. just writing it here because if anyone else faces the same problem, it will help them with that tiny issue.
Thanks again, @jamesramsay !