How to Suspend a Google Script to Avoid Limits

Google Script imposes quotas around different services. If your script exceeds the specified quota, it throws an exception and terminates execution until the quota is reset.

For instance, a Google Script can read 20,000 email messages from Gmail per 24 hours before it throws an exception like Service invoked too many times.

The Save Gmail addon downloads email messages from Gmail and writes them as PDF files to your Google Drive. It uses a time-based trigger to run the script in the background or a user can manually run the app to download emails.

If a user’s Gmail account has a large number of emails and they try to run the script too frequently, it could exceed the quota and the trigger may fail. It thus help to have some sort of checks in the script that will temporarily pause the script execution if a known exception if thrown.

const suspend = (timeInMinutes = 60) => {
  CacheService.getScriptCache().put('SUSPEND', Date.now(), timeInMinutes * 60);
};

const isSuspended = () => {
  return CacheService.getScriptCache().get('SUSPEND');
};

We are using the CacheService of Google Script to track if a script has been suspended.

The expiry time is set to 60 minutes so the script will automatically resume execution once the cache value has expired.

In the main app, we add a try catch block that parses the exception message. It the message matches one of the known errors - like Service using too much computer time for one day or Service invoked too many times - we pause the script for 60 minutes.

const app = () => {
  try {
    // download emails
  } catch ({ message }) {
    if (/Service invoked too many times/.test(message)) {
      suspend(60);
    }
  }
};

const hourlyTrigger = () => {
  if (!isSuspended()) {
    app();
  }
};

The next time our hourlyTrigger is invoked, it will run the main app only if the Google Script is not in suspended state. As we are using the Cache Service here, the suspended state is automatically reset when the cache expires.

Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.