Skip to content

Instantly share code, notes, and snippets.

@Rycochet
Created November 8, 2020 13:02
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 Rycochet/770657632f701fb328f359b8019d1b2a to your computer and use it in GitHub Desktop.
Save Rycochet/770657632f701fb328f359b8019d1b2a to your computer and use it in GitHub Desktop.
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