removeAccessInFolder(folderId, email, opts) looks at every file in a Drive folder and removes that person as an editor or viewer. It defaults to a dry run ({dryRun: true}): you get a report of what would change, and nothing is revoked until you pass {dryRun: false}. Pass {recursive: true} to include subfolders.
When to use
- Offboarding someone from a client or team folder
- A cleanup after you have audited who can see the files
- Any revoke you want to preview and log before it happens
How to use this snippet
/**
* Remove an editor or viewer from files in a folder.
* dryRun defaults to true. recursive defaults to false.
* Returns {dryRun, email, results[]} where each result has
* name, url, id, role, action, and error.
* Actions: would-remove-editor, would-remove-viewer, removed-editor,
* removed-viewer, skipped-owner, not-found, unavailable.
*
* @param {string} folderId
* @param {string} email
* @param {{dryRun?: boolean, recursive?: boolean}} [opts]
* @return {{dryRun: boolean, email: string, results: Array}}
*/
function removeAccessInFolder(folderId, email, opts) {
opts = opts || {};
var dryRun = opts.dryRun !== false; // default true
var recursive = !!opts.recursive;
var who = String(email || '').trim().toLowerCase();
if (!who) throw new Error('removeAccessInFolder: email required');
var folder = DriveApp.getFolderById(folderId);
var results = [];
function emails(users) {
return users.map(function (u) { return String(u.getEmail()).toLowerCase(); });
}
function describe(file) {
var row = {
name: file.getName(),
url: file.getUrl(),
id: file.getId(),
role: '',
action: 'none',
error: ''
};
try {
var owner = file.getOwner();
var ownerEmail = owner ? String(owner.getEmail()).toLowerCase() : '';
if (ownerEmail === who) {
row.role = 'owner';
row.action = 'skipped-owner';
results.push(row);
return;
}
var editors = emails(file.getEditors());
var viewers = emails(file.getViewers());
if (editors.indexOf(who) !== -1) row.role = 'editor';
else if (viewers.indexOf(who) !== -1) row.role = 'viewer';
else {
row.action = 'not-found';
results.push(row);
return;
}
if (dryRun) {
row.action = 'would-remove-' + row.role;
} else {
if (row.role === 'editor') file.removeEditor(who);
else file.removeViewer(who);
row.action = 'removed-' + row.role;
}
} catch (e) {
row.error = e.message;
row.action = 'unavailable';
}
results.push(row);
}
function walk(f) {
var files = f.getFiles();
while (files.hasNext()) describe(files.next());
if (recursive) {
var subs = f.getFolders();
while (subs.hasNext()) walk(subs.next());
}
}
walk(folder);
return { dryRun: dryRun, email: who, results: results };
}
Example
function previewOffboard() {
var report = removeAccessInFolder('FOLDER_ID', 'ex@example.com', { recursive: true });
Logger.log(report);
}
Notes
- The owner of a file cannot be removed this way; those rows come back as
skipped-owner. Transfer or rewrite ownership first if that is the real problem. getOwner,getEditors,getViewers,removeEditor, andremoveViewercan throw on files in a shared drive. Those rows areunavailablewith the error message; the rest of the folder is still processed.- Commenters show up in
getViewers()and are removed withremoveViewer. - Pair with List Folder Sharing if you want a full access dump before you revoke anyone.
Happy Coding!
