Maintain Inbox-Zero Using GoogleScript Automation for Gmail

Move Labelled Emails to Trash or Archive After a Configurable Number of Days

Varun Arora
9 min readJan 2, 2021

Reading all of the important and relevant emails in your inbox takes time. Some days you don’t even get the chance, creating a backlog of emails for the next day, which will have its own flood of new incoming emails too. Not to mention these pertinent emails are likely surrounded by less important or even spam emails in your inbox.

Organizing your emails as you read them takes even more time. Clicking, tapping, and swiping away at thread after thread to move them to folders, archive, or trash, over and over, day after day, becomes a huge time sink and a huge burden for anyone. Most likely you aren’t doing this for only one email account either. The end result? An inbox spiraling out of control and your goal of maintaining Inbox-Zero rendered futile.

What are some things we can do to avoid feeling helpless against email?

First Defense: Filters

Gmail already offers two management tools: Filters and Labels. You can set up filters to immediately act on new emails attempting to enter your Inbox. These actions can allow these emails to skip the inbox entirely and go to some other folder, including Archive and Trash. Filters can also attach a user-made label to emails based on your filter criteria, while choosing to skip the inbox or not. Sure, setting up filters and labels also takes time, but it’s a one time effort that helps you continuously once done.

But what about the emails that you don’t want skipping the inbox with a filter? Don’t you wish there was an “Archive after 5 days” or “Trash after 7 days” concept in Gmail? What if you could somehow make this happen for all kinds of emails that you want to see and read, but don’t want to spend the time moving around yourself?

Well, there’s a way to do it. It’s called GoogleScript. Below I will walk you through the steps of taking a GoogleScript I have written to move emails from the inbox to Archive and Trash after a customizable number of days based on Gmail labels. The one-time effort needed on your part to get this all working is to follow the steps below, and create Filters and Labels for all of the types of emails you would like to see in your inbox, and want to archive or trash at a later point in time, automatically.

Setting up the GoogleScript File

Head over to script.google.com, and make sure you are signed in with the same Gmail account whose inbox you’d like to manage. You’ll be met with the Apps Scripts home page for your Google Account. Go ahead and click on the New Project button.

You’ll now have a new blank script open, that looks something like below. There is also a new version of the script editor that looks different, but the functionality and general placement of the buttons is the same.

Go ahead and click on Untitled project and give it a name. I like to call mine “Gmail Message Mover”.

Lets also go ahead and rename the script from Code.gs to something better, like “GmailMessageMover”. You can hit the drop down arrow in the left-side navigation column and click Rename. The name of the file will change.

Now we’re ready to write (or in your case copy, paste and modify) a GoogleScript.

The Gmail Message Mover Script

In your script you see the below code.

function myFunction() {}

Go ahead and delete it so that your script file is completely blank. Now, copy and paste the below code into it.

var LABELS = [];
var ACTION_TIME_DAYS = [];
var ACTIONS = [];
// To Trash
CreateTarget("MyLabel1", 1, "Trash");
// To Archive
CreateTarget("MyLabel2", 1, "Archive");
function MoveEmails() {
var inbox_page_threads = GmailApp.getInboxThreads();
for (var i = 0; i < inbox_page_threads.length; ++i) {
var thread = inbox_page_threads[i];
var thread_labels = thread.getLabels();
if (thread_labels.length != 1) {
continue;
}
var label = thread_labels[0];
var label_index = LABELS.indexOf(label.getName());
if (label_index < 0) {
continue;
}

var action = ACTIONS[label_index];
var action_time = ACTION_TIME_DAYS[label_index];

var date = new Date();
var action_target = (date.getDate() - action_time);
date.setDate(action_target);

var message_date = thread.getLastMessageDate();
if (message_date > date) {
continue;
}

switch (action) {
case "Trash":
thread.moveToTrash();
break;
case "Archive":
thread.moveToArchive();
break;
}
}
}
/* Helpers */
function CreateTarget(label, action_after_days, action) {
LABELS.push(label);
ACTION_TIME_DAYS.push(action_after_days);
ACTIONS.push(action);
}

If we were to run the above script (don’t worry how to do this yet), it will:

  1. Find all the emails in your inbox
  2. Move all emails with the label “MyLabel1” older than 1 day (24hrs) to trash
  3. Archive all emails with the label “MyLabel2” older than 1 day (24hrs)

So, how do you modify this script to work for emails and labels that matter to you? How do you get the script running? Lets use a live example with my own inbox to see how it’s done.

Modifying GmailMessageMover.gs To Work For You

Let’s start this example with this email from Verizon Wireless I recently got on 12/29/2020 about my leftover data for the month.

Inbox View
Viewing the Email

I’d like the GoogleScript to move any email from Verizon Wireless to Trash after 2 days. Why would I want this? I know that I check my personal email daily, and I know that any email communication from Verizon Wireless isn’t relevant to me anymore after I read it. I’ll give myself 2 days to read it, just in case. I don’t want a Gmail filter to catch this email and have it skip my inbox immediately. What I do want is for this email to leave my inbox automatically, after some amount of time.

Label Setup

The GmailMessageMover.gs works using Gmail Labels. So what I will do is create a Gmail filter to automatically label all emails from Verizon Wireless as “Verizon”. That is all the filter does, it does NOT use the “Skip the Inbox (Archive It) option or the “Delete it” option.

Use the Gmail search to target all emails you want labelled with a particular label, and then click “Create filter”. My creation of “Verizon” is shown below.

In the filter, check the “Apply the label” box and select from an existing filter you have or create a new one. I will create a new one with “New label…”, name it “Verizon”, and apply this filter to all matching conversations.

Now my email (and all past emails from Verizon Wireless) has the Verizon label attached to it.

Viewing the same email again

Tell GmailMessageMover.gs to Use the Label

Now, we will create the following rule in our script “Any email in my inbox with the Verizon label that is older than 2 days ago, move it to trash”. The filter we made in Gmail will make sure to always mark emails from Verizon Wireless with the “Verizon” label. GmailMessageMover.gs will handle the rest.

We add the Verizon label, number of days, and action (Archive or Trash) by adding a new “CreateTarget” line to our script. I will add this below the current CreateTarget line that already has “Trash”, just to be organized. For emails+labels you want archived, do the same but the action will say “Archive”, just like MyLabel2 has it.

// To Trash
CreateTarget("MyLabel1", 1, "Trash");
CreateTarget("Verizon", 2, "Trash"); <-- newly added!
// To Archive
CreateTarget("MyLabel2", 1, "Archive");

Go ahead and delete the “MyLabel1” and “MyLabel2” CreateTarget lines, those were just examples for you to get started.

// To Trash
CreateTarget("Verizon", 2, "Trash");
// To Archive

Now we’re ready to get the script running.

Running GmailMessageMover.gs Daily

In order to run the script it will need two things:

  1. A rule (called a Trigger) on when to run and how often
  2. Permission to your Gmail

In your script, there is a toolbar. Click on the clock-thought-bubble icon to take you to the Trigger Management page for the script. You can also manage your triggers from the GoogleScript main page by clicking on “My Triggers”.

In the trigger manager, go ahead and click Create a New Trigger at the bottom of the page.

Making the Script Trigger

Using the drop down boxes fill them out to match what you see below. This is telling the script to run every day sometime between 10pm-11pm to clean up your labelled emails in your inbox.

One caveat to be aware of: The time that you run the script in the trigger matters. When the script runs and checks how many days old the email is, it is looking at not only the Month,Date,Year but the time elapsed of that email. This means that if you get recurring emails (like Google News Alerts) at 10am, any trigger before 10am will not see those emails as 1-day old. If you run the trigger at 11am instead, only then would 24 hours (1 day) have truly passed for that email. For this reason, I prefer to have my trigger set to 10pm, to capture all emails received before 10pm the previous day.

Another way to change this would be to only move emails that have been read, and don’t touch anything that is unread. Personally, I don’t want it that way because I don’t click and read all of my emails.

When you click Save, Google will ask you to grant the script permissions to your Gmail account.

Authorizing the Script

Go ahead and sign in with your Google Account, and you will see the below message:

Click Allow, and you will see the below message:

As long as the email that shows on this message is yours, no need to worry. Click the “Advanced” button in the bottom left to reach this next window:

And then click on “Go to Gmail Message Mover (unsafe)”. Again, as long as the email in the message is yours, this is completely safe.

The trigger will now save and you can see this in your dashboard:

Enjoy the Magic

On December 31st, my trigger launched the script just as it does every day since I set it up. This should have moved my Verizon Wireless data usage email to trash, because it had the label Verizon and was 2 days old (received 12/29/2020), and my CreateTarget line stated the action was “Trash”.

And indeed that is what happened:

Other Remarks

The script shared in this post only works with emails that have one user-defined label. (Don’t worry, “Inbox” doesn’t count as a user-defined label). This means that for emails that have multiple labels on them, the script doesn’t know what to do so it will just skip those. You can go ahead and add new logic to the script to handle emails with multiple labels in whatever way you wish for them to be handled.

The script does not permanently delete emails. For all emails intended to be trashed, the script moves them to Trash and Gmail will clear out the Trash every 30 days. This is so that if there is a one-off email that you want back in the inbox, you can restore it and drop it’s label.

If you want to make changes to the script or see what else can be done with GoogleScript and Gmail you may read the documentation. https://developers.google.com/apps-script/reference/gmail/gmail-app

--

--

Varun Arora
Varun Arora

Written by Varun Arora

Frequent thinker. Infrequent writer.

No responses yet