Auto-Archive Emails in Gmail

Goal

I'm looking for a job in software engineering and have signed up for job alerts from LinkedIn.

I still, however, have a personal training business to run, and these emails are muddying my inbox, making it easier to miss important emails from my clients.

Given these job alerts are time-sensitive, I am okay with archiving emails that have been in my inbox for three days.

A good alternative would be to check these emails right away, but hey, sometimes I get busy!

Starting Point

I'm starting with the following snippet from Amit Agarwal.

function cleanInbox() {

    var delayDays = 2;

    var maxDate = new Date();
    maxDate.setDate(maxDate.getDate()-delayDays);

    var threads = GmailApp.getInboxThreads();

    for (var i = 0; i < threads.length; i++) {
        if (threads[i].getLastMessageDate() function archiveInbox() {
            var threads = GmailApp.search('label:inbox is:read older_than:2d');
            for (var i = 0; i < threads.length; i++) {
                threads[i].moveToArchive();
            }
    }

Google Apps Script Project Setup

  1. Head to https://script.google.com
  2. Make sure you're logged in with the account associated with the Gmail inbox you want to clean
  3. Click on "+ New Project"
  4. Click on the "+" button next to Services and add "Gmail" to give the script permission to use Gmail, an advanced service.

Your script will be written inside the code.gs file that automatically opens when creating a new project.

Search and Archive Script

Open up code.gs under Files and enter your script. Here's how I adjusted Amit's code to serve my purposes:

function archiveInbox() {
    var threads = GmailApp.search('label:inbox from:([email protected]) older_than:3d');
    for (var i = 0; i < threads.length; i++) {
        threads[i].moveToArchive();
    }
}

Read the documentation to learn more about the GmailApp. For example, you may want to skip archiving and move the thread straight to the trash.

A Note on Feature Design

When searching for the features I wanted, it was helpful to take a step back from the Gmail Service documentation and think about how I would accomplish this task using the standard Gmail graphical user interface.

For my situation, I would use search to filter emails by sender and date. I figured out who the sender was and used the date filtering similar to Amit's example.

Test Your Script

Once you think your code is correct, click on the "Save" button and then the "Run" button.

An Execution log should open at the bottom. If everything worked, you should see "Execution started" and "Execution completed".

You can add console.log("message") statements to tell you what's happening when the code is run.

Enable Time Trigger

As it stands, the script is actually done.

The issue is that it's not saving any time to have to open up Google Apps Script every time you want to archive a few emails.

To run this script automatically, click on "Triggers" with the time clock icon in the leftmost sidebar.

Then click "+ Add Trigger" and choose your settings. Mine were like this:

  • Choose which function to run on: archiveInbox
  • Choose which deployment should run: Head
  • Select event source: Time-driven
  • Select type of time based trigger: Day timer
  • Select time of day: Midnight to 1am
  • Failure notification settings: Notify me daily

If you changed the function name or you have multiple, they should show up in this dialog. You can customize the time to fit your needs.

Troubleshooting

When first running, Apps Script will tell you "Authorization required". You must review and allow permissions using Google Single Sign-On.

If you forgot to add the Gmail service, the execution log will tell you that "Execution started", but it will hit an error of "Script function not found".

If you haven't placed your script within a function, you will not be able to run it.

If the code appears to run correctly, but you do not notice the desired outcome in your Gmail inbox, double check that the project was created under the correct account.

Conclusion

I'm seeing the power of Google Apps Script as someone using many Google services. Perhaps we can start utilizing Google Sheets and Docs more?

Get Notified of New Posts

Sign up for the newsletter and I'll send you an email when there's a new post.