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.
https://www.googleapis.com/auth/drivefunction 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
Step-by-Step Setup & Deployment
- 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()orsetValue()inside loops. Read ranges into memory in 1 call, process arrays in V8 memory, and commit changes with a singlesetValues()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
Auto-Organize Google Drive Uploads into Year / Month Subfolders
Scans an incoming upload folder in Google Drive, examines file creation dates, and moves files into organized subfolders formatted as YYYY/MM.
Google DriveAutomated Daily Spreadsheet Backup to Google Drive Archive
Creates a point-in-time timestamped backup copy of a critical production Google Sheet inside a dedicated backup folder every night.
Google DriveAudit Google Drive for Files Larger than 50MB and Export to Sheet
Scans Google Drive storage, identifies massive videos, zip archives, and bloated presentations, and logs file name, owner, and size into a spreadsheet for storage cleanup.