I don’t know about you, but I receive tons of emails each day in my Gmail inbox. Some are my fault; I had signed up for a newsletter on a whim because it was something I was actually interested in (like sales at Michael’s or DSW), and others might just be a result of third-party companies selling your data for profit and you end up on some list that’s kind of related to a thing you bought once on eBay three years ago.

I used to be vigilant in sorting my email. I’d go through everything and delete promotions that were no longer in date, archive reports on accounts from weeks ago, and sort my client emails in the inbox into folders. But then it started taking longer and longer to sort my Gmail. There was just too much! So I started to unsubscribe or update my preferences to be less frequent, but I still had thousands of emails to delete or archive, so what to do?

First, I looked at the Spam and Trash folder since they already had this feature. I had hoped to find a checkbox or something in my settings to simply enable it. Unfortunately, this was not the case. Next, I searched Google. While I am a developer, part of my mantra includes not reinventing the wheel. Surely I’m not the first person to want this, so maybe someone already created something to help!

I came upon this Gmail Auto Purge tool by Digital Inspiration, written in Google Scripts. It is a very simple tool that simply takes a Gmail label and the number of days and then it just deletes emails based on those parameters. However, it didn’t encompass all of my needs. For starters, I need to also automatically archive emails in the Updates category because I don’t want to delete them forever (like digital receipts for tax purposes), and secondly, it only used the “label” parameter in the search and I needed to use “category”. Thankfully, Digital Inspiration posted their source code for the script, so it gave me a starting point. Here is the criteria I wanted to be able to change about their script:

  1. Ability to choose a “Do This” action, even if it’s just a boolean between deleting and archiving. Ideally, I’d like to have the same options as I do when creating a filter.
  2. Ability to use any search query, specifically “category:promotions”, “category:updates”, and “category:social”.
  3. Change how often it runs from once a day to every week or even once a month. As a developer, I feel like I need to consider processing power, and I don’t particularly care if I have an email in my Promotions category that is exactly 30 days and 5 hours old. It can wait until the script runs again every 3 days or so to delete that one.

So I modified the script and created my own. Now without further ado, get organized by completing the form below to receive a copy of the Gmail Auto-Organize script, then follow the installation instructions!

Installation Instructions

  1. Create a copy of the Gmail Auto-Organize script into your Google Drive by completing the form above.
  2. Update the value of the string variable (text that appears within double quotes) called search_term to whatever you would use to search your email. Any search operator is valid. Because I’m targeting the Promotions tab, I would change it to “category:promotions”.
  3. Set the various actions of what you want to do with the emails that meet these parameters to true or false. Descriptions of what these actions do are listed below.
  4. Update the value of days to how old you want the email to be when it gets automatically sorted. I set this to 30 so it only deletes Promotional emails that are 30 days old or older. In case you were wondering, this script is using the older_than search operator in conjunction with your search parameters in step 2 to filter your emails.
  5. Update the value of repeat to how often you want this script to run. I set mine to 7 so it only runs this script and auto-deletes Promotional emails once a week.
  6. Click on Run -> Authorize to allow the script to access your email and manage it.
  7. Click on Run -> Install to start it up.
  8. Optional: Click on Run -> Sort Gmail to run the script immediately if you don’t want to wait ;)

And that’s all! If you want to turn it off, just open the script up in your Google Drive and click on Run -> Uninstall.

Actions Explained

  • If you want these emails to be sent to the Trash, change the value of action_delete to true. I set this to true because I want to delete the Promotions emails.
  • If you want these emails to be archived (shoved into the All Mail area of your account), change the value of action_archive to true. I set this to false because I only want to delete the Promotions emails.
  • If you want to add a label to these emails, change the value of action_addLabel from false to the name of the label you want to add, surrounded by double quotes (like in search_term). I set this to false because I only want to delete the Promotions emails.
  • If you want to remove a label from these emails, change the value of action_removeLabel from false to the name of the label you want to add, surrounded by double quotes (like in search_term). I set this to false because I only want to delete the Promotions emails.
  • If you want these emails to be sent to your Inbox, change the value of action_inbox to true. I set this to false because I only want to delete the Promotions emails.
  • If you want these emails to marked as important, change the value of action_important to true. I set this to false because I only want to delete the Promotions emails.
  • If you want these emails to marked as unimportant, change the value of action_unimportant to true. I set this to false because I only want to delete the Promotions emails.
  • If you want these emails to marked as read, change the value of action_read to true. I set this to false because I only want to delete the Promotions emails.
  • If you want these emails to marked as unread, change the value of action_unread to true. I set this to false because I only want to delete the Promotions emails.
  • If you want these emails to be sent to your Spam, change the value of action_spam to true. I set this to false because I only want to delete the Promotions emails.