1

Does anyone have any suggestions for how to remove a file from shared with me? I need to do this without actually revoking access, as the people are added via a contact group, so only removing 1 person will not work.

Things to note:

I know using this can find the file:

var files = DriveApp.searchFiles('sharedWithMe');
//then I set file = what I'm looking for.  

However, this will not remove the file from shared with me:

DriveApp.removeFile(file);

I've also tried brute forcing by patching the metadata, except it was to no avail. In addition, it is wrong to assume that shared with me is a folder, as when listing parent folders the file returns nothing.

Nikolay Fominyh
  • 8,946
  • 8
  • 66
  • 102
Ryan Toner
  • 91
  • 1
  • 6

1 Answers1

0

Only DriveApp cannot completely remove files. So it is necessary to use Drive API. "removeFile()" is used for changing parent folder information. It cannot remove files. I prepared 2 samples for removing files. If you are owner of the file, you can remove it.

Move file to trash box and empty trash :

var file = DriveApp.getFilesByName("filename").next();
file.setTrashed(true)
Drive.Files.emptyTrash();

Remove directly file :

Drive.Files.remove("fileID");

It was confirmed that above script can remove file even if other users are using.

For using above script, it is necessary to enable Drive API at Advanced Google Services and Drive API at https://console.developers.google.com/ Developers Console Project.

Files without parent folder :

If you had used "removeFile()", files without the parent folder may be existing in your drive. You can confirm them following script. This can retrieve file name and file ID of files without the parent folder.

var result = [];
var files = DriveApp.getFiles();
while (files.hasNext()) {
  file = files.next();
  if (!file.getParents().hasNext()) {
    result.push([file, file.getId()]);
  }
}
Logger.log(result);
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • I am not the owner of the file- so it is impossible to remove it using Drive.Files.remove(). I am looking to remove the file from the shared with me section of google drive, similar to automating the function of right clicking the file and hitting remove. I have those APIS enabled. – Ryan Toner Feb 27 '17 at 05:29
  • If you are not owner for the file, although you cannot remove completely it, you can hide the file from your Drive using this. ``var file = DriveApp.getFilesByName("filename").next(); var folder = file.getParents().next(); folder.removeFile(file);`` In this case, you can confirm using "Files without parent folder:" script on my answer. – Tanaike Feb 27 '17 at 05:38
  • Error: Cannot retrieve the next object: iterator has reached the end. I'm thinking that shared with me is not actually a parent. – Ryan Toner Feb 27 '17 at 05:41
  • This [link](http://stackoverflow.com/questions/29084127/remove-a-file-that-was-previously-added-from-shared-with-me-to-my-drive) is actually my question, although it seems they cannot find an answer either. – Ryan Toner Feb 27 '17 at 05:41
  • The script works fine. Can I ask you about the script the error occurs? The script removes the parent folder information from the shared file. – Tanaike Feb 27 '17 at 05:44
  • The script fails at file.getParents().next() because .next() returns null. For some reason 'sharedWithMe' does not seem to be classified as a parent; consequently, there is not parents returned. – Ryan Toner Feb 27 '17 at 05:51
  • sharedWithMe file doesn't have the parent folder information. It can be searched. https://developers.google.com/drive/v3/web/search-parameters But the parent cannot be changed. I'm sorry I couldn't help. – Tanaike Feb 27 '17 at 06:03