How to Use Google Workspace Drive API to Monitor Shared Folder Growth

When a Google Workspace environment scales, Shared Drives (formerly Team Drives) can quickly become dumping grounds. A “Marketing Assets” Shared Drive might start with 50 GB of organized collateral, but over two years, it balloons to 4 TB of uncompressed video files and duplicated archives. Google’s Admin Console shows total domain storage, but tracking the growth rate of specific Shared Drives over time requires external tooling. By utilizing Google Apps Script and the Drive API, you can build an automated system that calculates the size of specific folders and logs the data daily, allowing you to identify out-of-control storage usage before it becomes a billing nightmare.

Step 1: Set Up the Logging Sheet

You need a place to store the historical data so you can build charts later.

  1. Create a new Google Sheet named “Storage Growth Tracker”.
  2. In the first row, create three column headers: Date, Folder Name, and Size in GB.
  3. Go to Extensions > Apps Script to open the code editor.

Step 2: Enable the Advanced Drive Service

The basic DriveApp.getFolderById() class cannot easily calculate the size of every file recursively within nested subfolders. We will use the Advanced Drive API to query file sizes directly.

  1. In the Apps Script editor, click the + next to Services.
  2. Select Drive API (v3) and click Add.

Step 3: The Calculation Script

Calculating the size of a Shared Drive is tricky because folders themselves don’t have a “size” attribute; only the files inside them do. Furthermore, the API limits how many files it returns per query, so we must handle pagination (using pageToken).

Delete the default code and paste this script:

function logSharedDriveSize() {
  // Replace with your Shared Drive ID
  const DRIVE_ID = 'YOUR_SHARED_DRIVE_ID_HERE'; 
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  
  let totalBytes = 0;
  let pageToken = null;
  let driveName = "Unknown Drive";

  try {
    // Get the name of the Shared Drive for the log
    const driveInfo = Drive.Drives.get(DRIVE_ID);
    driveName = driveInfo.name;
    
    // Query all files within the specific Shared Drive
    do {
      const response = Drive.Files.list({
        corpora: 'drive',
        driveId: DRIVE_ID,
        includeItemsFromAllDrives: true,
        supportsAllDrives: true,
        fields: 'nextPageToken, files(size)',
        pageSize: 1000,
        pageToken: pageToken
      });
      
      const files = response.files;
      for (let i = 0; i < files.length; i++) {
        // Only files have a size; Google Docs/Sheets technically report as 0 bytes in API v3
        // but binary files (PDFs, MP4s, ZIPs) report their exact byte size.
        if (files[i].size) {
          totalBytes += parseInt(files[i].size, 10);
        }
      }
      
      pageToken = response.nextPageToken;
    } while (pageToken); // Continue if there are more pages of files

    // Convert bytes to Gigabytes (1 GB = 1073741824 bytes)
    const totalGB = (totalBytes / 1073741824).toFixed(2);
    
    // Log the result to the sheet
    const today = new Date();
    sheet.appendRow([today, driveName, totalGB]);
    
  } catch (e) {
    Logger.log('Error calculating size: ' + e.message);
  }
}

Step 4: Execute and Authorize

  1. Replace YOUR_SHARED_DRIVE_ID_HERE with the actual ID (found in the URL when you open the Shared Drive in your browser).
  2. Click the Run button.
  3. Google will prompt you to authorize the script to read your Google Drive files. Grant the necessary permissions.
  4. Check your Google Sheet. You should see a new row with today’s date and the exact size of the Shared Drive in Gigabytes.

Step 5: Automate the Execution (Time-Driven Triggers)

To monitor growth, this script needs to run automatically without your intervention.

  1. In the Apps Script editor, look at the left sidebar and click on the Triggers icon (it looks like a small alarm clock).
  2. Click + Add Trigger in the bottom right corner.
  3. Set the following options:
    • Choose which function to run: logSharedDriveSize
    • Select event source: Time-driven
    • Select type of time based trigger: Day timer
    • Select time of day: Choose a time, like Midnight to 1am.
  4. Click Save.

Your script will now wake up every night, calculate the total size of the Shared Drive, and add a new row to your spreadsheet. After a few weeks, you can highlight the data in the spreadsheet and insert a Line Chart to instantly visualize whether your storage growth is linear, exponential, or leveling off.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.