Created
November 8, 2020 13:02
-
-
Save Rycochet/770657632f701fb328f359b8019d1b2a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function gmailAutoarchive() { | |
const delayDays = 2; | |
const batchSize = 100; | |
const maxDate = new Date(); | |
maxDate.setDate(maxDate.getDate() - delayDays); | |
const threads = GmailApp.search(`in:inbox has:userlabels older_than:${delayDays}d`).filter((thread) => thread.getLastMessageDate() < maxDate); | |
if (threads.length) { | |
Logger.log(`Found ${threads.length} threads:`); | |
for(var i = 0; i < threads.length; i++) { | |
Logger.log(` ${i+1}: "${threads[i].getFirstMessageSubject()}"`); | |
} | |
while (threads.length) { | |
var batch = threads.splice(0, Math.min(threads.length, batchSize)); | |
GmailApp.moveThreadsToArchive(batch); | |
} | |
} else { | |
Logger.log(`No threads to archive`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment