Google Drive AutomationDifficulty: Advanced⚡ Weekly Trigger

Audit & Revoke External Sharing Permissions on Old Drive Files

Scans Google Drive for files shared with external public links created more than 90 days ago, and restricts sharing back to private access to uphold data security.

Required OAuth Scopes: https://www.googleapis.com/auth/drive
Code.gs (Apps Script V8 Runtime)
function auditAndRestrictExternalShares() {
  const cutoffDays = 90;
  const now = new Date();
  const cutoffTime = now.getTime() - (cutoffDays * 24 * 60 * 60 * 1000);
  
  const files = DriveApp.searchFiles('visibility = "anyoneWithLink"');
  let revokedCount = 0;
  
  while (files.hasNext()) {
    const file = files.next();
    if (file.getDateCreated().getTime() < cutoffTime) {
      file.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.NONE);
      revokedCount++;
    }
  }
  
  Logger.log('Revoked public link sharing on ' + revokedCount + ' stale files.');
}

Line-by-Line Code Explanation

Line 2-4: Calculates 90-day epoch millisecond threshold.
Line 6: Searches files configured with public link access (`anyoneWithLink`).
Line 11-13: Reverts public link access back to Private for files exceeding cutoff date.

Step-by-Step Setup & Deployment

  1. Run in Apps Script to audit public link exposure across company drive accounts.

Best Practices for Quota & Rate Limit Management

When deploying this automation in Google Workspace, adhere to these operational principles:

  • Batch Reads & Writes: Never execute getValue() or setValue() inside loops. Read ranges into memory in 1 call, process arrays in V8 memory, and commit changes with a single setValues() call.
  • Execution Timeouts: Apps Script terminates scripts exceeding 6 minutes. For multi-thousand row datasets, implement batch pagination with resume timestamps stored in PropertiesService.
  • OAuth Permissions: Scope your manifest (appsscript.json) narrowly to prevent security prompts from alarming external team members.

Frequently Asked Questions

Is Google Apps Script completely free to use?

Yes. Google Apps Script is built into all free Google accounts and Google Workspace editions. There are no server fees or subscription costs for running scripts within daily quotas.

Can I run this script on an automated schedule without keeping my computer on?

Yes. Time-driven triggers execute on Google's cloud infrastructure. Your script will run reliably even when your computer is closed or offline.

More Google Drive Automation Snippets