Earlier this year John Day posted about using time-based Gmail filters with Google Apps Script to aut…

Earlier this year John Day posted about using time-based Gmail filters with Google Apps Script to automatically archive and delete mail after a certain amount of time, since Google introduced the tabbed inbox I have used Johns example functions to come up with a function that will automatically delete mail from the Promotions category after 1 month and a second function that will automatically archive mail from the Social, Updates and Forums categories after 1 month.

function batchDeletePromotions() {
  var batchSize = 100 // Process up to 100 threads at once
  var threads = GmailApp.search('category:promotions older_than:1m');
  for (j = 0;j < threads.length;j+=batchSize) {
    GmailApp.moveThreadsToTrash(threads.slice(j,j+batchSize));
  }
}

function batchArchiveCategories() {
  var myCategories = {'"updates"':"1m",'"forums"':"1m",'"social"':"1m"};  
  var batchSize = 100; // Process up to 100 threads at once
  for(aCategory in myCategories) {
    var threads = GmailApp.search('label:inbox category:'+aCategory+' older_than:'+myCategories[aCategory]+'');
    for (j = 0;j < threads.length;j+=batchSize) {
      GmailApp.moveThreadsToArchive(threads.slice(j,j+batchSize));
    }
  }  
}

Check out John's post for the details about how to use them at http://www.johneday.com/422/time-based-gmail-filters-with-google-apps-script

Back To Top