Skip to content

Using Google Apps Scripts

Versión en español de esta publicación.
If you like email as much as me and you use GMail as your primary email provider it is possible that you already had the same problem as I had some time ago. It turns out that in GMail it is possible to organize your email using labels that is the same analogy as organize files in folders. In my case, I have my email organized using multiple labels and using also sub labels and I’m a heavy fan of GMail filters which can automatically apply a label to a new email that arrive and put that email in specific “folder”.

This GMail filtering system is very useful and powerful. The problem raise when you have to mark all email in some of the labels as read. And I say problem because the real problem comes up when trying to mark all email in a label and all the corresponding sub labels at any level as read automatically. And this is simply not supported by GMail.

Google Apps Scripts to the Rescue
It turns out that Google has a variety of APIs for all its products that can be used to interact with them from a defined language, in this case Javascript. The APIs are quite friendly and easy to use. Also, the documentation is excellent and highly understandable. And not to do the long story, I created a script to do just that, mark as read all emails under a defined tag including all child tags.

function markLabelThreadsAsRead(label) {
  var labels = GmailApp.getUserLabels();
  for (var i = 0; i < labels.length; i++) {
    var index = labels[i].getName().indexOf(label, 0);
    if (index === 0) { // if label name starts with parent label name
      Logger.log("processing label:" + labels[i].getName());
      // batch processing ref:
      var batchSize = 100 // Process up to 100 threads at once
      var threads ="label:" + labels[i].getName() + " is:unread");
      for (j = 0; j < threads.length; j+=batchSize) {
        Logger.log("mark thread as read:" + threads[j].getFirstMessageSubject());
        GmailApp.markThreadsRead(threads.slice(j, j+batchSize));


To run the script simply visit the Google online editor ( ), paste the script there and then you can press the run or debug option. The APIs documentation can be found here

Enjoy! =)

Leave a Reply

Your email address will not be published. Required fields are marked *