Skip to content

Instantly share code, notes, and snippets.

Created January 8, 2017 16:39
Show Gist options
  • Star 74 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save anonymous/2cca33d376f7f924fdaa67891ad098cc to your computer and use it in GitHub Desktop.
Save anonymous/2cca33d376f7f924fdaa67891ad098cc 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();
}
}
}
@bo33b
Copy link

bo33b commented Feb 21, 2024

@101legend101 this is my approach, quite simple really. Cheers.

function gmailAutoarchive() {

  // Build the query
  var query = "in:inbox is:read has:userlabels older_than:2d";
  
  // Get the threads
  var threads = GmailApp.search(query);
  
  // Archive the threads
  for (var thread of threads) thread.moveToArchive();

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment