Search code examples
google-apps-scriptgmailgmail-api

250 / 500 threads limit in GmailApp with Google Apps Script


I noticed the following limitation when using GmailApp in Google Apps Script:

var threads = GmailApp.search("to:[email protected]");
Logger.log(threads.length);  // 250 max

var threads = GmailApp.search("label:mylabel");
Logger.log(threads.length);  // 500 max

var label = GmailApp.getUserLabelByName('mylabel');
var threads2 = label.getThreads();
Logger.log(threads2.length); // 500 max

How would you make a job (such as extracting email adresses and adding them to a list) on more than 500 or 250 threads?

Would you do it manually by splitting by dates (not very beautiful but probably working)?


Solution

  • You could loop over the result with a max of e.g. 100 and stop when the length of the resulting threads is less than max:

    var max = 100;
    var offset = 0;
    var searchThreads = [];
    
    while (true) {
      var threads = GmailApp.search("to:[email protected]", offset, max);
      searchThreads = searchThreads.concat(threads);
      if (threads.length < max) {
        break;
      }
    
      offset += max;
    }