Tutorial: Teach your email inbox to organize itself

What to do when you need your email inbox to organize itself

I have a bad habit that I’m trying to break. It goes like this: I get an idea that I would be a better person, calmer, or more productive if I just added some tiny habit to my daily life. Sometimes it’s exercise, sometimes it’s a new style of meditation, writing, reading, eating, cleaning, or organizing. It always seems like such a good idea, and such a tiny change that it’s impossible for me to fail. However, these “tiny” changes pile on top of each other and overwhelm me, and not only do I fail to make it a daily habit, I abandon my other daily habits as well, and end up sleeping in, watching Netflix late at night, and skipping yoga classes that I really did want to attend. I set unrealistic expectations for myself, and end up doing nothing.

Well-intentioned suggestions for what we “should” be doing can be found everywhere. It’s easy to feel that in order to live our best lives, we have to change the way we know we function best. This is not true.[Tweet this!] Everything that we implement in our lives to help us should ultimately be making it easier for us to relax and do what we enjoy more often — this includes technology and new habits.

Awhile ago, I wrote an article that was quite popular and seemed to help a lot of people, about “how to organize your email and resolve deeper issues which are holding you back“. The suggestions I made in that article reflect the way that I, personally, use my email inbox, and I was thrilled that the article was shared so many times.

However, I have come to realize that some people don’t use their email inbox the way I do (shocker!). Some people use their email much more minimally – they read their email, they receive notifications through social media sites, and once in awhile they use it to communicate with family and friends. They don’t spend a lot of time on email, they don’t necessarily have a consistent method of organization, and there are hundreds, or perhaps even thousands, of emails in their inbox. They probably just want their email to take care of itself.

If this sounds like you, I have a plan of action for you. It will take a bit of time to set up (about 30 minutes), but after that, the system will do its work in the background without you lifting a finger. Your email will, essentially, organize itself.


You’ll need a Gmail account to use this method.


This tutorial will lead you through a few steps to implement a Google Apps Script (GAS) that will make the following things happen in your inbox:

  1. All email older than 14 days (or however many days you want email to stay in your inbox) will be automatically archived in “All Mail” (not deleted). This means that it will be searchable using the email search bar on top of your Gmail account, but it won’t be sitting in your inbox anymore.
  2. All email that is not longer in your inbox will be marked as “Read”.

Step 1

Make sure you are logged in to your Gmail account in your browser. Navigate to “Google Apps Script” and click on “Start Scripting”.

Google Scripts: Start Scripting

Step 2

Click on “Untitled project” and rename it “Archive Email Script”.

Change project name

Step 3

Delete everything you see in the Code.gs window (usually “function myFunction() { }). Then, copy and paste the entire code below into the document.

function batchMarkasread() {
 var batchSize = 100 // Process up to 100 threads at once
 var threads = GmailApp.search('is:unread -label:"inbox"');
 for (j = 0; j < threads.length; j+=batchSize) {
 GmailApp.markThreadsRead(threads.slice(j, j+batchSize));

function archiveInbox() {
// Every thread in your Inbox that is older than fourteen days.
var threads = GmailApp.search('label:inbox older_than:14d');
 for (var i = 0; i < threads.length; i++) {
[/highlighted_text] Optional: If you want to change the amount of time an email stays in your inbox, simply edit the number in following line of code to reflect the number of days you’d like.

var threads = GmailApp.search(‘label:inbox older_than:14d’);

Step 4

[row padding_top=”” padding_bottom=”” bg=”” bg_light=”true” appear=”false”]

Run the script. Under “Run”, click “myFunction”.

Screen Shot 2016-04-25 at 12.52.20 PM

Authorize it to run by clicking “Continue”.

Screen Shot 2016-04-25 at 12.52.34 PM




[row padding_top=”” padding_bottom=”” bg=”” bg_light=”true” appear=”false”]

Click “Agree” when it asks you for permission to View and Manage your Email.

Screen Shot 2016-04-25 at 12.52.45 PM

You will see a red notification at the top of the page – that’s ok! It’s normal. You can ignore it or dismiss it.

Screen Shot 2016-04-25 at 12.52.57 PM


Step 5

Click on “Run” again – this time, it will show the two scripts that you copied and pasted from this site. Click on one, then the other, to run each one in turn. A yellow notification box will appear at the top of the page, then disappear, if the scripts are working properly. Yay!

Screen Shot 2016-04-25 at 12.53.05 PM

Step 6

Time to set up timed “triggers” for both scripts. Click on the little clock icon in the toolbar.

Screen Shot 2016-04-25 at 12.53.52 PM

[row padding_top=”” padding_bottom=”” bg=”” bg_light=”true” appear=”false”]

This is what you’ll see. Click to set up a new trigger.

Screen Shot 2016-04-25 at 12.54.02 PM

It’ll set up a trigger for the first function. That’s good! Now click on “Add a new trigger.”

Screen Shot 2016-04-25 at 12.54.12 PM




It’ll create the exact same trigger you already have. Just click on the name of the function and select “archiveInbox”. Then click “Save”.

Screen Shot 2016-04-25 at 12.54.24 PM

The script processes email in “batches” of 100 at a time. Once the script is set up, it’ll take a few hours or days for your entire email inbox to be processed according to the script. After that, though, it’ll be kept beautifully clean at all times!

Step 7

You are done – your inbox will now automatically archive email after your set number of days, and mark all email that’s not in the inbox as “Read”. Congratulations!

You can now find any email you need to reference using the search bar at the top of the page in Gmail. You can also use filters and labels to keep track of any email you need to access more readily.

Did you like it? Share it!