I created two scripts on my script.google.com account that are helpful for managing email. Thanks to this post on the Official Gmail Blog by Corey Goldfeder. I was able to coble together two very useful functions. A delayed delete function that allows me to flag emails for deletion at a future date but still remain in my inbox for a fixed length of time. A snooze email feature that allows me to have flagged emails return to my inbox unread at a date in the future.

Both of these features allow me to make a decision about the email immediately and then not worry about it again. The flag for deletion is nice because it allows me to hang on to low priority emails for a while just in case they become relevant, but in the likely hood they are never relevant they will be cleaned out after they expire.

Setup/Installation

Setup and installation is pretty simple. I have both scripts in the same project script.google.com project. Once you have logged into the script.google.com site you can create a new project from the File menu. In the new project you will need to turn on the Gmail API from the Resources menu under Advanced Google Services… and then select it from the list.

You can copy the scripts I have provided below into two separate scripts or a single script (create a new script from the File menu). Then you can manually run the setup functions once to create the required labels in your gmail account. After that you need to setup two triggers. From the buttons you should see a clock looking icon. In the corresponding dialog box you can create one daily trigger for each of the two functions, deleteEmail() and moveSnooze().

The Gmail API seems to struggle with lots of threads in a label so this one is set to just process a chunk of 100 threads each time it’s run. You can run it about once an hour and it will more than likely keep up with normal usage cases.

Delayed Delete

var KEEP_FOR_DAYS = 100;
var DELETE_LABEL = 'Delayed Delete';

function setupDelayedDelete() {
  GmailApp.createLabel(DELETE_LABEL);
}

function deleteEmails(){
  var threads = GmailApp.search('l:'+DELETE_LABEL+' older_than:'+KEEP_FOR_DAYS+'d',0,100);
  if(threads.length > 0){
    var now = new Date().getTime();
    for(var i = 0;i<threads.length;i++){
      var date = threads[i].getLastMessageDate().getTime();
      if((now - date)/86400000 > KEEP_FOR_DAYS){
        threads[i].moveToTrash();
      }
    }
  }
}

Email Snooze

var MARK_UNREAD = true;
var ADD_UNSNOOZED_LABEL = false;

function getLabelName(i) {
  return "Snooze/Snooze " + i + " days";
}

function setupSnooze() {
  // Create the labels we’ll need for snoozing
  GmailApp.createLabel("Snooze");
  for (var i = 1; i <= 7; ++i) {
    GmailApp.createLabel(getLabelName(i));
  }
  if (ADD_UNSNOOZED_LABEL) {
    GmailApp.createLabel("Unsnoozed");
  }
}

function moveSnoozes() {
  var oldLabel, newLabel, page;
  for (var i = 1; i <= 7; ++i) {
    newLabel = oldLabel;
    oldLabel = GmailApp.getUserLabelByName(getLabelName(i));
    page = null;
    // Get threads in "pages" of 100 at a time
    while(!page || page.length == 100) {
      page = oldLabel.getThreads(0, 100);
      if (page.length > 0) {
        if (newLabel) {
          // Move the threads into "today’s" label
          newLabel.addToThreads(page);
        } else {
          // Unless it’s time to unsnooze it
          GmailApp.moveThreadsToInbox(page);
          if (MARK_UNREAD) {
            GmailApp.markThreadsUnread(page);
          }
          if (ADD_UNSNOOZED_LABEL) {
            GmailApp.getUserLabelByName("Unsnoozed")
              .addToThreads(page);
          }          
        }     
        // Move the threads out of "yesterday’s" label
        oldLabel.removeFromThreads(page);
      }  
    }
  }
}