Skip to content

Instantly share code, notes, and snippets.

@deviationist
Last active May 9, 2023 09:28
Show Gist options
  • Save deviationist/8c1b845fba9bb7afb149db4c83b83330 to your computer and use it in GitHub Desktop.
Save deviationist/8c1b845fba9bb7afb149db4c83b83330 to your computer and use it in GitHub Desktop.
This is a script that will traverse all your Google Drive files and remove all sharing (owner access only).
// Ensure you've installed gdrive first: https://github.com/glotlabs/gdrive
import { spawnSync } from 'child_process';
const delimiter = ';;;';
let files = [];
function getRawFiles(parent) {
const args = ['files', 'list', `--field-separator=${delimiter}`];
if (parent) {
args.push(`--parent=${parent}`);
}
return spawnSync('gdrive', args).stdout.toString();
}
function getFiles(parent) {
const files = getRawFiles(parent);
return files.split('\n').filter(e => e).slice(1).map(e => {
const keys = ['id', 'name', 'type', 'size', 'created'];
const values = e.split(`${delimiter}`);
return keys.reduce((obj, key, index) => ({ ...obj, [key]: values[index] }), {});
});
}
function getFilesRecursive(parent) {
const filesInFolder = getFiles(parent);
for (const file of filesInFolder) {
console.log(file);
files.push(file);
if (file.type == 'folder') {
getFilesRecursive(file.id);
}
}
return files;
}
function removeAllSharing(file) {
spawnSync('gdrive', ['permissions', 'revoke', '--all', file.id]);
}
const fileTree = getFilesRecursive();
for (const file of fileTree) {
removeAllSharing(file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment