Skip to content

Instantly share code, notes, and snippets.

@m4rcsch
Forked from anonymous/gmailAutoarchive.js
Created January 2, 2018 10:17
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 m4rcsch/bea2e7666e4f533b4b5691a085317ce4 to your computer and use it in GitHub Desktop.
Save m4rcsch/bea2e7666e4f533b4b5691a085317ce4 to your computer and use it in GitHub Desktop.
function gmailAutoarchive() {
var delayDays = 2; // will only impact emails more than 48h old
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays); // what was the date at that time?
// Get all the threads labelled 'autoarchive'
var label = GmailApp.getUserLabelByName("autoarchive");
var threads = label.getThreads(0, 400);
// we archive all the threads if they're unread AND older than the limit we set in delayDays
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate)
{
threads[i].moveToArchive();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment