title: Exception: Service invoked too many times for one day: gmail
body: <p>We have a very simple Google Script that deletes any emails older than 90 days for a GSuite email account. It is set to run every hour due to the undocumented 500 result limit on <a href="https://developers.google.com/apps-script/reference/gmail/gmail-app#search(String)" rel="nofollow noreferrer">gmailApp.Search</a> and needing to delete thousands of emails (roughly 7500/day):</p>
  
  <pre><code>function deleteOldEmail() {
      var threads = GmailApp.search('in:inbox older_than:90d'); 
  
      Logger.log('Found: ' + threads.length + ' Emails');
  
      var batchSize = 100
      for (j = 0; j &lt; threads.length; j+=batchSize) {
          GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize));
      }
  
      Logger.log('Deleted: ' + j + ' Emails');
  }
  </code></pre>
  
  <p>Under "My Executions" shows that this is constantly failing with the message:</p>
  
  <blockquote>
    <p>Exception: Service invoked too many times for one day: gmail. at deleteOldEmail(Code:4:26)</p>
  </blockquote>
  
  <p>Going to the Google Cloud Platform (GCP) dashboard by selecting Resources->Cloud Platform Project and clicking the name of the project shows the error on the Error Reporting card, and there are API requests on the APIs card. When clicking "Go to APIs Overview" the resulting page is showing no information at all: </p>
  
  <p><a href="https://i.stack.imgur.com/wFrFv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wFrFv.png" alt="enter image description here"></a></p>
  
  <p>I need to determine which quota is being hit, and how. Clicking on the Apps Script API from the above page shows no traffic and no data under quotas. Clicking on the Gmail API from the above page shows no traffic and no data under quotas. Under IAM &amp; Admin->Quotas there is 0 usage for all APIs listed. </p>
  
  <p>How does one determine what quota is being hit? </p>
  
  <p><strong>EDIT:</strong> I turned on email notices for failures on the trigger. It said it was Line #12, which is the GmailApp.moveThreadsToTrash() method. Its not the search that was the issue as some other posts have questioned (without answer). </p>
  
  <p>The script is now running again as of noon today, so I will be able to count the number of times it runs multiplied by 500 and get a rough idea. I will post an update when it happens as there are a lot of posts that I have found regarding this with no solution. </p>
  
  <p><strong>EDIT 2:</strong> Well, unfortunately I have yet to hit the limit again. The script has deleted just shy of 11K in 24 hours at its peak. </p>