Skip to content

Instantly share code, notes, and snippets.

@KamiinBlack
Forked from anonymous/gmailAutoarchive.js
Last active October 2, 2019 13:05
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 KamiinBlack/3ae947d0663fa332dee13adc4eee069b to your computer and use it in GitHub Desktop.
Save KamiinBlack/3ae947d0663fa332dee13adc4eee069b to your computer and use it in GitHub Desktop.
//version with unread check and nested labels
function gmailAutoarchive() {
var delayDays = 1; // will only impact emails more than 24h old
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays); // what was the date at that time?
// Get all the threads labelled 'label_child2'
var threads = GmailApp.search("in:inbox label:label_parent-label_child1-label_child2");
// 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].isUnread()==false)
{
threads[i].moveToArchive();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment