What is the real purpose of DriveApp.removeFile?

2020-07-24 15:10发布

问题:

Google AppScript is quite an useful service but there is a mechanism for files deletion that I don't understand. The same discussion applies to folder removal. I've replied some questions on SO where file removal was required, but there is still something that is not clear to me.

The DriveApp documentation explains the method DriveApp.removeFile:

Removes the given file from the root of the user's Drive. This method does not delete the file, but if a file is removed from all of its parents, it cannot be seen in Drive except by searching for it or using the "All items" view.

I tried to inspect the behavior of such method with some testing script like this one:

function remove() {
  var files = DriveApp.getFilesByName("file_to_remove");
  while (files.hasNext()) 
    DriveApp.removeFile(files.next());
}

but the result of this code is to remove only visually the file from my Drive web-interface (I'm not using clients), but the file (as from the documentation) still exist in "All Items".

Another way to remove the file is by setting it as trashed, thus it is actually removed the first time I clear the trash bin manually:

function trash() {
  var files = DriveApp.getFilesByName("file_to_remove");
  while (files.hasNext()) 
    files.next().setTrashed(true);
}

but still, I do not like this approach (the file is still using part of my - extremely limited :P - quota).

A final method (that is what I'm using right now) employes the Advanced Google Services, for example:

function advanced_remove() {
  var files = DriveApp.getFilesByName("file_to_remove");
  while (files.hasNext()) 
    Drive.Files.remove(files.next().id);    
}

But what I don't like is the usage of the Services (that are limited in number of operations). Also it is possible through the Drive object to empty the trash bin (Drive.emptyTrash(), if I'm using the previous example)

IMHO the file removal is quite a basical operation that should be available through standard DriveApp API, and not by employing different approaches that do not completely remove the file or do require me to manually empty the trash.

My questions are:

  • what is the real purpose of removeFile? And what means removed from all of its parents (I'm assuming parents are folders that contain the files)?
  • is there any actual way to remove completely the file without passing by the trash bin and without using advenced services?
  • I'm not a native English speaker: Maybe Google Developers have implemented the methods considering a "semantic" difference between remove and delete?

回答1:

Think of Google Drive "folders" as labels attached to a file, the way that we attach labels in Gmail. This is what they actually are, they do not function like directories on a hard drive. A file may exist that does not have any labels, or it can have multiple labels. The method folder.remove(file) removes the label of that folder from the drive. And DriveApp.remove(file) does the same but for the label "My Drive". Being "removed from all of its parents" means you remove all labels from the file, including My Drive. You still have the file.

is there any actual way to remove completely the file without passing by the trash bin and without using advanced services?

No, there is not. Scripted file deletion is a dangerous operation, considering that scripts are shared and some of them are malicious. So Google decided to limit the possible damage in this way, by not allowing outright deletion through GAS without explicitly enabled advanced services.



回答2:

DriveApp.removeFile just remove the given file from the root of the user's Drive. This method does not delete the file, but if a file is removed from all of its parents, it cannot be seen in Drive except by searching for it or using the "All items" view. As stated also in this thread, there is no direct way in GAS to delete files without sending them to trash nor to empty the trash.