Auto-expire and revoke old sharing links
Tighten access after a set number of days — revoke link sharing on Northwind files older than 90 days.
Published Jul 16, 2025
“Anyone with the link” is a brilliant way to share a file quickly and a quiet liability six months later. Northwind hands out link-shared documents to clients and contractors all the time, and almost none of those links ever get turned off. The access just lingers, long after the work is done.
This script puts an expiry date on that openness. It walks a folder, finds files that have not been touched in 90 days, and revokes any “anyone with the link” sharing on them — flipping access back to private. Active files are left exactly as they are; only the stale, forgotten ones get locked down.
What you’ll need
- The Drive folder ID you want to audit. The script takes it as an argument, so you can point it at different folders without editing the code.
- Edit access to the files in that folder — the script can only change sharing on files you own or can manage.
- A view on what counts as “stale”. The default is 90 days since the last update; adjust the constant below to suit.
The script
// A file is considered stale once it has gone this many days without
// an update.
const STALE_DAYS = 90;
/**
* Walks a folder and revokes "anyone with the link" sharing on every
* file that has not been updated within STALE_DAYS.
*
* @param {string} folderId ID of the Drive folder to audit.
*/
function revokeOldLinks(folderId) {
// 1. Work out the cutoff timestamp: anything older than this is stale.
const cutoff = Date.now() - STALE_DAYS * 86400000;
const files = DriveApp.getFolderById(folderId).getFiles();
let revoked = 0;
// 2. Walk every file in the folder.
while (files.hasNext()) {
const file = files.next();
// 3. Skip files updated recently — they are still in active use.
if (file.getLastUpdated().getTime() > cutoff) continue;
// 4. Only act on files shared by link. Files shared with named
// people, or already private, are left untouched.
const access = file.getSharingAccess();
if (access === DriveApp.Access.ANYONE_WITH_LINK
|| access === DriveApp.Access.ANYONE) {
// 5. Revoke link sharing: access becomes private, no permission.
file.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.NONE);
revoked++;
}
}
Logger.log('Revoked link sharing on ' + revoked + ' stale file(s).');
}
How it works
revokeOldLinkscomputes a cutoff timestamp by subtractingSTALE_DAYS(90) worth of milliseconds from the current time.- It opens the folder by the ID passed in and gets an iterator over its files.
- For each file it checks
getLastUpdated(). If the file was modified after the cutoff it is still active, so the loopcontinues and leaves it alone. - For a stale file it reads
getSharingAccess(). Only files set toANYONE_WITH_LINKorANYONE(publicly discoverable) are targeted — files shared with named collaborators are deliberately untouched. - It calls
setSharingwithAccess.PRIVATEandPermission.NONE, which removes the link-sharing entirely so the file is reachable only by people explicitly added to it. - It logs how many files were locked down so each run leaves an audit trail.
Example run
Suppose the audited folder holds four files when the script runs:
| File | Last updated | Sharing | Outcome |
|---|---|---|---|
Q1-pitch.pdf | 130 days ago | Anyone with link | Revoked — now private |
client-brief.docx | 200 days ago | Anyone with link | Revoked — now private |
live-budget.xlsx | 4 days ago | Anyone with link | Kept — updated recently |
team-notes.doc | 150 days ago | Named people only | Kept — not link-shared |
The log reads Revoked link sharing on 2 stale file(s). — the two old,
link-shared files are now private, while the actively edited budget and the
already-restricted notes are left as they were.
Run it
Call revokeOldLinks with the folder you want to audit. The simplest way is a
tiny wrapper, which is also what a trigger can point at:
function auditClientFolder() {
revokeOldLinks('1abcClientFolderId');
}
To keep the audit running on its own, add a time-based trigger:
- In the Apps Script editor open Triggers (the clock icon).
- Add a trigger for
auditClientFolder, Time-driven, Week timer, on a day that suits you.
A weekly sweep is usually enough — link sharing does not become a risk overnight.
Watch out for
- This is one-way and silent. A revoked link simply stops working, and nobody is told. If a client suddenly cannot open a file, a recently expired link is the likely cause — re-share it explicitly with the people who still need it.
- Only the top level of the folder is scanned. Files in sub-folders are not
touched. Recurse into
getFolders()if you need a deep audit. - “Last updated” is not “last viewed”. A file nobody has opened in a year but that was tweaked last week counts as active. Drive’s API does not expose a reliable last-viewed time.
setSharingcan throw for files you do not own, or for items inside a shared drive with managed sharing. Wrap the call in atry/catchif the folder is a mix of owners.- Test on a throwaway folder first. Revoking access across a real client folder by mistake is awkward to undo one file at a time.
Related
Detect and report broken file shortcuts
Find Drive shortcuts in Northwind folders pointing at deleted or inaccessible files.
Updated Dec 3, 2025
Build a Drive cleanup recommendation report
Suggest what Northwind can delete or archive — large, stale, duplicate, or untouched files.
Updated Nov 21, 2025
Generate a folder-level changelog
Track additions and deletions in a Northwind folder over time — a written history.
Updated Nov 5, 2025
Track contract expiry from Drive files
Read expiry dates out of Northwind contract Docs and warn before renewals.
Updated Oct 28, 2025
Build a Drive quota early-warning system
Alert Northwind before storage runs out — email when usage crosses 80%.
Updated Oct 20, 2025