Skip to content

Instantly share code, notes, and snippets.

@aikar aikar/purgeGmail.gs
Last active Jun 16, 2019

Embed
What would you like to do?
// Modified from: http://www.labnol.org/internet/gmail-auto-purge/27605/
// Labels to support
// Delete after 15 days
// Delete after 30 days
// Delete after 90 days
var days = [15,30,90];
function Intialize() {
return;
}
function Install() {
ScriptApp.newTrigger("purgeGmail").timeBased().everyDays(1).create();
}
function Uninstall() {
var triggers = ScriptApp.getScriptTriggers();
for (var i=0; i<triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function purgeGmail(purge) {
days.forEach(function(purge) {
var label = "delete-after-" + purge + "-days";
var age = new Date();
age.setDate(age.getDate() - PURGE_AFTER);
var purge = Utilities.formatDate(age, Session.getScriptTimeZone(), "yyyy-MM-dd");
var search = "label:\"" + label + "\" before:" + purge;
try {
var threads = GmailApp.search(search, 0, 500);
for (var i=0; i<threads.length; i++) {
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) {}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.