Automatic Newsletter Cleanup in Gmail

If you haven’t tried Google Apps Script, I found a really nifty use for it: smart filtering for email. Wait, shouldn’t I just use Gmails' built-in filters? As it turns out you can’t - my filter needs to act on email that matched that filter in the past. So in other words: a filter can only act on email it actually “filters”, which kinda makes sense! I’m a big fan of automation (and email is ripe for automation), as you can see from my post on meetings in Outlook.

The source is below, all you need to do is replace the mailing list identifiers at the start (see the comments). You can find these easily enough in Gmail by using the dropdown menu on the email and selecting “Filter mails like this”:

Selecting a newsletter mail’s menu to filter

This will prefill the search with the mailing list id you need:

Gmail Filter Search

function processInbox() {
   var mailingLists = [ // replace the strings in this array with your own!
     "list:'faa8eb4ef3a111cef92c4f3d4mc list <>'", 
     "list:'e2e180baf855ac797ef407fc7mc list <>'",
     "list:'9f4b80a35728f7271fe3ea6ffmc list <>'"
  for(var i = 0; i < mailingLists.length; i++){
    // process all threads in the Inbox
    var threads = searchMessages(mailingLists[i]);
    if(threads.length > 1){
      Logger.log("Found: " + threads.length + " messages to process, for " + mailingLists[i]);
      var msgToSort = getMessagesFromThreads(threads);
      msgToSort.sort(function(a,b) { 
        return new Date( - new Date(;
      msgToSort.splice(0, 1); // Remove latest mail
      for (var i = 0; i < msgToSort.length; i++) {
        Logger.log("Moving message: " + msgToSort[i].subject + " to trash");

function searchMessages(filter){

function getMessagesFromThreads(threads){
 var msgsToSort=[];
    for (var i = 0; i < threads.length; i++) {
      // get all messages in a given thread
      var messages = threads[i].getMessages();
      for (var j = 0; j < messages.length; j++) {
  return msgsToSort;

function processMessage(message) {
  var subject = message.getSubject();
  var date = message.getDate();
  Logger.log(date + " " + subject);
  return {"subject":subject, "message": message, "date": date }

Also available as an easily cloneable (with git) Gist on Github.