Skip to content

Instantly share code, notes, and snippets.

@maesoser
Last active October 16, 2018 14:31
Show Gist options
  • Save maesoser/995ff84643e24620c46fbb58268c79b2 to your computer and use it in GitHub Desktop.
Save maesoser/995ff84643e24620c46fbb58268c79b2 to your computer and use it in GitHub Desktop.
Auto archive old threads that have not unreaded emails
/*
Este script mantiene limpia mi bandeja de entrada, archivando correos antiguos
*/
function gmailAutoArchive() {
gmailAutoarchiveHelper("Informes",3);
gmailAutoarchiveHelper("Sistemas",3);
gmailAutoarchiveHelper("Diseños",3);
gmailAutoarchiveHelper("inbox",7);
}
function gmailAutoarchiveHelper(queryStr, numDays) {
Logger.log('Running archiver on label %s for numDays: %s',queryStr, numDays);
var query = "label:"+ queryStr + " AND label:inbox AND -is:starred AND older_than:" + numDays +"d";
var threads = GmailApp.search(query,0, 250).filter(function(thread) {
// Only return if the thread does not have any unread messages.
return (!thread.isUnread());
// return (thread.isInInbox() && !thread.isUnread() && !thread.isImportant());
});
if(threads.length!=0){
Logger.log('Found %s email threads belonging to %s to be archived', threads.length, queryStr);
while (threads.length) {
var this_batch_size = Math.min(threads.length, 250);
var this_batch = threads.splice(0, this_batch_size);
GmailApp.moveThreadsToArchive(this_batch);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment