Tonight I’m Cleaning Out My Inbox (sort of)

Tonight I’m Cleaning Out My Inbox (sort of)

by Daniel J. VandeBunte

The Problem

Have you heard of this new thing called “email?” I’m not sure it’s going to catch on, but I know some people disagree with me. And in the interest of full disclosure I must admit that I have been known to use email on occasion for work. But it is such a pain to manage. Every thread going into the same inbox. Sure, I could set up some labels and filters to help sort threads automatically. But what about threads in my inbox that do not fit into my predetermined labels? How can I manage those threads without going through the several hundreds of them individually and deciding what to do with them?

The Solution

Since I use Gmail for both my personal and work email accounts, I have the option of managing my inbox by using Google Apps Script. I already have several labels and filters set up so the script would only need to manage threads not previously sorted by those. Here’s what I would want my script to do; gather the unlabeled threads from my inbox, check the date of the last email in the thread, and archive any thread where the last message is older than some given number of days. Lastly, I want the program to run itself frequently enough to keep my inbox fairly clean. Since archived threads still show up in search results, archiving old threads only removes them from my inbox, it does not delete them.

The Program

The source code for the apps script can be found here: autoArchiveGmail.

The purpose of this program is to automatically archive Gmail threads older than 60 days. I always try to name functions after what they do, so this function is named autoArchiveGmail. The 60 days can be customized once you make a copy of the script.

naming the function

naming the function

function autoArchiveGmail() {

The first step I need the function to take is to gather the threads in my inbox.

get the threads from my inbox

get the threads from my inbox

var threads = GmailApp.getInboxThreads();

Next, I need the function to determine a point of reference it can use to compare to the age of the last message in the thread. This is easily done by creating a new Date value when the function is run.

create a reference to right now

create a reference to right now

var now = new Date();

Next, I need to define within the function exactly what I mean by “old.” In my example, I use 60 days. To change this for yourself, just edit this line of code to the number of days you prefer. Since this script is not bound to another doc, the only way to change this value is to open the source code and change it.

set the number of days

set the number of days

var xDays = 60;

Since math with date variables is typically done in milliseconds, I need to convert 60 days into an equivalent number of milliseconds.

set milliseconds

set milliseconds

var milliSecs = (1000*60*60*24*xDays);

Next, the function needs to check each thread in my inbox to see if the newest message is older than 60 days. To do this, I use a for loop. A for loop is a conditional loop that executes a series of commands if certain conditions are met. In this case, the commands are executed for each thread that was gathered earlier.

loop through each thread

loop through each thread

To accomplish this, I create an index variable i, check to see if the value of i corresponds to the location of a thread in the threads array. If it does, the commands are executed. If it does not, i.e. i exceeds the number of threads, the commands are not executed and the loop stops. Since location in array variables starts with 0, i is set to 0, not 1. This also explains why the commands are only executed if i < threads.length. The value of threads.length is the number of threads in the threads variable. This is different than the location of any thread in the threads array.

create the for loop

create the for loop

for (var i = 0; i < threads.length; i++) {

Next, for every value of i that corresponds to a thread in the threads array variable, the date of the last message in the thread is checked.

check last message date

check last message date

var threadDate = threads[i].getLastMessageDate();

Once the date of the last message has been collected, it must be determined if the date is older than 60 days. This can be done by comparing the date of the last message with the value of the now variable defined earlier. If it is older than 60 days, I want to archive the thread. To simplify this process, I put it in a conditional statement that will only execute the command to archive if the last message is older than 60 days.

archive threads older than 60 days

archive threads older than 60 days

if (now - threadDate > milliSecs) {
threads[i].moveToArchive();
}

To set the trigger, find the menu just above the script editor and click on the icon that looks like a clock.

look for the trigger icon

look for the trigger icon

Once the trigger popup opens, you will have several options for setting a trigger.

set the script's trigger

set the script’s trigger

Click Save when you are done setting the trigger the way you want.


Leave a Reply

Your email address will not be published. Required fields are marked *