Skip to content

Instantly share code, notes, and snippets.

@fearphage
Created October 2, 2018 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fearphage/3903e977ccdcc8d2ca3c992dc68e07fe to your computer and use it in GitHub Desktop.
Save fearphage/3903e977ccdcc8d2ca3c992dc68e07fe to your computer and use it in GitHub Desktop.
Archives (removes from inbox) emails tagged with "expires" tag that are more than 10 days old
// The name of the Gmail Label to expire
var GMAIL_LABEL = 'expires';
// Archive messages automatically after N days
var EXPIRE_AFTER = '10';
/*
* STOP EDITING BELOW HERE UNLESS
* YOU KNOW WHAT YOU'RE DOING
*
*/
function Install() {
ScriptApp.newTrigger('purge')
.timeBased()
.after(1000 * 60 * 5)
.create()
;
ScriptApp.newTrigger('purge')
.timeBased()
.everyDays(1)
.create()
;
}
function Uninstall() {
var
triggers = ScriptApp.getProjectTriggers()
,i = 0
,trigger
;
while (trigger = triggers[i++]) {
ScriptApp.deleteTrigger(trigger);
}
}
function purge() {
var
expiry = new Date
,i = 0
,threads
,thread
;
expiry.setDate(expiry.getDate() - EXPIRE_AFTER);
threads = GmailApp.search('in:inbox label:' + GMAIL_LABEL + ' before:' + Utilities.formatDate(expiry, Session.getScriptTimeZone(), 'yyyy-MM-dd'), 0, 100);
if (threads.length == 100) {
ScriptApp.newTrigger('purge')
.timeBased()
.after(1000 * 60 * 5)
.create()
;
}
while (thread = threads[i++]) {
if (thread.getLastMessageDate() < expiry) {
thread.moveToArchive();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment