0
votes

I would like my script to be able to unshare all folders, subfolders and files in my Google Drive. So far it only unshares specific files by using the folder id. Help is much appreciated

function RemoveEditors(){
 var files = DriveApp.getFolderById("1uus-Cv3n1xxQbSyWakQwqp7vNtTJGnWe").getFiles();
 while (files.hasNext()) {
  var file = files.next()
  var docs = DriveApp.getFileById(file.getId());
  var users = docs.getEditors();
      for (i in users) {
        email = users[i].getEmail();
	    if (email != "") {
	     docs.removeEditor(email);

}
}
}
1
You can post code in your question. That would be better.Alan Wells

1 Answers

1
votes

Try:

function RemoveEditors(){
  var files = DriveApp.getFiles();
  while (files.hasNext()) {
    var file = files.next()
    var docs = DriveApp.getFileById(file.getId());
    var users = docs.getEditors();
    for (i in users) {
      email = users[i].getEmail();
      if (email != "") {
        docs.removeEditor(email);

      }
    }
  }
}

Using getFiles() will allow you to get all of the files in the drive, then you can do the same process you had before.

EDIT

After reviewing the question I noticed you wanted to remove all perimssions, edit and view included, so I'm updating the code to go over all files & folders and remove both view and edit permissions, I'm leaving the old code as a reference. The new code:

function removePermissions(){
  var files = DriveApp.getFiles();
  var folders = DriveApp.getFolders();

  while (files.hasNext()) {
    var file = files.next()
    var docs = DriveApp.getFileById(file.getId());

    var editors = docs.getEditors();
    var viewers = docs.getViewers();
    var email;

    for (i in editors) {
      email = editors[i].getEmail();
      if (email != "") {
        docs.removeEditor(email);
      }
    }
    for (i in viewers) {
      email = viewers[i].getEmail();
      if (email != "") {
        docs.removeViewer(email);
      }
    }
  }
    while (folders.hasNext()) {
    var folder = folders.next()
    var docs = DriveApp.getFolderById(folder.getId());

    var editors = docs.getEditors();
    var viewers = docs.getViewers();
    var email;

    for (i in editors) {
      email = editors[i].getEmail();
      if (email != "") {
        docs.removeEditor(email);
      }
    }
    for (i in viewers) {
      email = viewers[i].getEmail();
      if (email != "") {
        docs.removeViewer(email);
      }
    }
  }
}