Created
October 2, 2018 20:34
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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