Skip to content

Instantly share code, notes, and snippets.

@burak-kara
Last active June 6, 2024 10:04
Show Gist options
  • Save burak-kara/b257525ea3ca781aa48982cc444fcdd0 to your computer and use it in GitHub Desktop.
Save burak-kara/b257525ea3ca781aa48982cc444fcdd0 to your computer and use it in GitHub Desktop.
Google Drive - Remove Sharing & Access Permissions from folders, subfolders and files. Note that Google Apps Script commands can be changed after this version.
// Note that this script will remove all permissions of all files and subfolders
// (including files in subfolders) of the given folder
// https://drive.google.com/drive/folders/abcdefgh
const id = "abcdefgh";
function start() {
const folder = DriveApp.getFolderById(id);
getSubFolders(folder);
// At the end, reset for parent folder
resetPermissions(folder);
}
// Get subfolders recursively
function getSubFolders(parentFolder) {
const childFolders = parentFolder.getFolders();
handleFiles(parentFolder);
handleFolders(childFolders, parentFolder)
}
// Handle files in a folder
function handleFiles(folder) {
const files = folder.getFiles();
if (files.hasNext()) {
Logger.log("Files are found in " + folder.getName());
while(files.hasNext()) {
resetPermissions(files.next());
}
} else {
Logger.log("No file in " + folder.getName());
}
}
// Handle subfolders of a folder
function handleFolders(folders, parent) {
if (folders.hasNext()) {
Logger.log("Child Folders are found for " + parent.getName());
while(folders.hasNext()) {
const child = folders.next();
resetPermissions(child);
getSubFolders(child);
}
} else {
Logger.log("No more subfolder in " + parent.getName());
}
}
function resetPermissions(asset) {
try {
if (asset) {
Logger.log("Name: " + asset.getName());
removeSharing(asset);
removeEditPermissons(asset);
removeViewPermissons(asset);
} else {
Logger.log("Not found! -error");
}
} catch (e) {
Logger.log(e.toString());
}
}
// Set sharing options for the asset (file or folder)
function removeSharing(asset) {
asset.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.NONE);
asset.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.NONE);
}
// Remove all users who have edit permissions
function removeEditPermissons(asset) {
const editors = asset.getEditors();
if (editors.length > 0) {
editors.forEach(editor => {
const email = editor.getEmail();
if (email != "") {
asset.removeEditor(email);
}
});
Logger.log("Editors are removed!");
} else {
Logger.log(asset.getName() + " does not have any editor");
}
}
// Remove all users who have view permssions
function removeViewPermissons(asset) {
const viewers = asset.getViewers();
if (viewers.length > 0) {
viewers.forEach(viewer => {
const email = viewer.getEmail();
if (email != "") {
asset.removeViewer(email);
}
});
Logger.log("Viewers are removed!");
} else {
Logger.log(asset.getName() + " does not have any viewer");
}
}
@Diegustos
Copy link

Tutorial please. I am new in this thing.

@burak-kara
Copy link
Author

burak-kara commented Jun 15, 2022

Tutorial please. I am new in this thing.

I believe you can find one on YouTube. The description states that Google Apps Script commands can be changed after this version. Therefore, this script might not be working

@mmansion
Copy link

Great starting point thank you

@JakubAndrysek
Copy link

Excellent, thanks 😍

@HackettLai
Copy link

You are just a genius!! Thx x 1000000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment