|
// The name of the Gmail Label that is to be checked for purging? |
|
var LABELS_TO_DELETE = [ |
|
"your-label-here" |
|
]; |
|
|
|
var TRIGGER_NAME = "dailyDeleteGmail"; |
|
|
|
// Purge messages in the above label automatically after how many days? |
|
var DELETE_AFTER_DAYS = "60"; |
|
|
|
var TIMEZONE = "AEST"; |
|
|
|
// Maximum number of threads to process per run |
|
// Note: this cannot be greater than 500, because Google reasons |
|
var PAGE_SIZE = 500; |
|
|
|
// If number of threads exceeds page size, resume job after X mins (max execution time is 6 mins) |
|
var RESUME_FREQUENCY = 7; |
|
|
|
// First trigger will run this many minutes after Install function is executed |
|
var INSTALL_DELAY = 2; |
|
|
|
/* |
|
IMPLEMENTATION |
|
*/ |
|
function Intialize() { |
|
return; |
|
} |
|
|
|
function Install() { |
|
|
|
// First run trigger |
|
ScriptApp.newTrigger(TRIGGER_NAME) |
|
.timeBased() |
|
.at(new Date((new Date()).getTime() + 1000*60*INSTALL_DELAY)) |
|
.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 { |
|
|
|
// Google Scripts has a stupid 20 trigger limit, which you can easily exceed if |
|
// there's a lot of mail that needs deleting. There's also no way that I know of |
|
// to find out the trigger execution date, name, run status or anything else useful from |
|
// invoking getProjectTriggers that might allow you to delete expired triggers. |
|
|
|
// Therefore, the only thing you can do to guarantee that you will not run out of |
|
// triggers is to stupidly delete ALL triggers and recreate the daily trigger. |
|
// It is dumb, but dumb is the way here. |
|
|
|
// Get rid of all triggers |
|
Uninstall() |
|
|
|
// Intstall daily trigger... Sigh. |
|
ScriptApp.newTrigger(TRIGGER_NAME) |
|
.timeBased().everyDays(1).create(); |
|
|
|
var threads = GmailApp.search(search, 0, PAGE_SIZE); |
|
|
|
// Resume again in RESUME_FREQUENCY 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) { |
|
// Never logs anything... Mysterious... Logs to Narnia? |
|
Logger.log("Deleting: " + email.getSubject()) |
|
email.moveToTrash(); |
|
} |
|
} |
|
} |
|
} |
|
|
|
} catch (e) { |
|
Logger.log(e); |
|
throw(e); |
|
} |
|
} |