Software and Other Stuff

28 Mar 2014

Fun with Google's Apps Script! Batch Processing Gmail emails By Host

Like everybody that has had the same email acount for a while, I receive a lot of emails. Most of them are automatic responses or update from sites like,,,, etc.. Others are mailing lists I signed up for and lost interest in, and so on.

Sometimes I get tired of the amount of emails to manage, and I just want to tell Gmail: "delete all emails coming from", for any number of hosts.

My email managemenet workflow goes something like this:

  1. Locate by visual inspection a host I want to delete or archive
  2. Search "from:host", mark all emails for deletion
  3. Delete all marked emails
  4. Repeat for as many hosts as needed.

Since it is unlikely Gmail will ever include a feature to automate such specific workflow, I decided to give Google's Apps Script a try (not to confuse with AppleScript :p).

I got tired of doing this by hand and I wrote a script to automate this task.

Here's how the resulting Apps Script looks like (you need to be logged into Gmail for it to work).

The script is also able to archive emails coming from a certain host, and/or mark them as important.

The standard disclaimer applies: this script will erase some of your emails! Normally it should only move emails from the hosts you select to your Trash folder, but use it at your own risk!

Developing this script was fun but I found a few caveats because I was lazy and decided not to use the ui services. Instead, I used the html services.

Apps Script uses caja under the hood, so a lot of things are not allowed (e.g.: it did not allow me to add pictures, like a loading gif, or svg, to the page).

All in all it was a fun experience and I think AppScript is definitely a very useful tool to have in your toolbox if you use Google products like Gmail or Spreadsheets.

Related Posts

07 Oct 2013
» Graphs are Everywhere! An overview of GraphConnect San Francisco 2013
23 Jul 2013
» Using AngularJS with a Rails backend
21 Apr 2012
» Tidying up an rspec suite with helpers

The opinions expressed in this personal blog represent my own and not those of my employer. All data and information provided is for informational purposes only. This blog makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.  |  icons