appscript.dev
Automation Beginner Drive Gmail

Build a Drive quota early-warning system

Alert Northwind before storage runs out — email when usage crosses 80%.

Published Oct 20, 2025

A full Drive is a quiet emergency. Uploads start failing, shared documents stop syncing, and the first anyone hears of it is a colleague asking why their file will not save. By then it is a scramble. Northwind would much rather get a gentle heads-up while there is still room to clear space or buy more.

This script checks the account’s storage quota and emails a warning the moment usage crosses 80%. It runs on a daily timer, costs nothing when there is plenty of room, and turns a sudden outage into a calm, planned cleanup.

What you’ll need

  • The advanced Drive API service enabled in the Apps Script project (Services → add Drive API) — Drive.About.get lives there.
  • A Gmail account to send from — the script uses GmailApp under your authorisation.
  • The address that should receive the warning, set in ALERT_RECIPIENT below.

The script

// Where the warning email is sent.
const ALERT_RECIPIENT = '[email protected]';

// Percentage of quota that triggers a warning.
const WARN_THRESHOLD_PCT = 80;

// Bytes in a gigabyte, for readable figures in the email.
const BYTES_PER_GB = 1024 * 1024 * 1024;

/**
 * Checks Drive storage usage and emails a warning if it has
 * crossed WARN_THRESHOLD_PCT. Designed to run on a daily trigger.
 */
function checkQuota() {
  // 1. Ask the Drive API for the storage quota figures.
  const about = Drive.About.get({ fields: 'storageQuota' });
  const limit = parseInt(about.storageQuota.limit, 10);
  const used = parseInt(about.storageQuota.usage, 10);

  // 2. Some accounts (e.g. pooled storage) report no fixed limit.
  if (!limit) {
    Logger.log('No fixed storage limit reported — nothing to check.');
    return;
  }

  // 3. Work out how full the account is.
  const pct = (used / limit) * 100;

  // 4. Below the threshold, do nothing — no email, no noise.
  if (pct < WARN_THRESHOLD_PCT) {
    Logger.log('Drive at ' + pct.toFixed(1) + '% — under threshold.');
    return;
  }

  // 5. Over the threshold: send a plain-spoken warning.
  GmailApp.sendEmail(
    ALERT_RECIPIENT,
    `Drive at ${pct.toFixed(1)}% capacity`,
    `Used ${(used / BYTES_PER_GB).toFixed(1)}GB of ` +
    `${(limit / BYTES_PER_GB).toFixed(1)}GB.\n\n` +
    `Clear some space or upgrade storage before uploads start failing.`
  );
  Logger.log('Warning sent — Drive at ' + pct.toFixed(1) + '%.');
}

How it works

  1. checkQuota calls Drive.About.get and asks only for the storageQuota field, keeping the request small.
  2. It parses the limit and usage values, which the API returns as strings of bytes.
  3. If limit is empty — some Workspace accounts use pooled storage with no per-user cap — it logs a note and stops, since there is nothing to measure.
  4. It divides usage by limit to get a percentage.
  5. If that is under WARN_THRESHOLD_PCT, it logs the figure and exits quietly — no email on a normal day.
  6. If it is at or over the threshold, it emails ALERT_RECIPIENT with the percentage and the used-of-total figure in gigabytes, plus a one-line nudge.

Example run

On a day when the account is comfortable, the log reads:

Drive at 62.4% — under threshold.

No email is sent. When usage climbs past 80%, the recipient gets:

Subject: Drive at 83.7% capacity

Used 12.6GB of 15.0GB.

Clear some space or upgrade storage before uploads start failing.

Trigger it

Storage fills gradually, so a daily check is plenty:

  1. In the Apps Script editor, open the Triggers panel (the clock icon).
  2. Click Add Trigger.
  3. Choose checkQuota, event source Time-driven, type Day timer, and pick an early-morning hour.
  4. Save. Approve the authorisation prompt the first time it runs.

Watch out for

  • The script warns once per day, every day, while usage stays above the threshold — so a busy account can produce a steady drip of emails until someone clears space. That repetition is intentional pressure, but raise WARN_THRESHOLD_PCT or send only on first crossing if it grates.
  • Drive.About.get reports the quota for the account running the script, not for a whole domain. For organisation-wide storage, use the Admin SDK instead.
  • Pooled-storage Workspace accounts may report no limit. The guard handles that gracefully, but it also means this script cannot warn those accounts — monitor pooled storage from the Admin console.
  • Trash still counts against quota until it is emptied. If the warning fires and usage looks wrong, empty the trash before buying more storage.

Related