Search code examples
google-apps-scriptgoogle-drive-shared-drive

Cannot remove a file from Team Drive with Apps Script


The following code works fine on Team Drive:

var files = folder.getFiles();

while (files.hasNext())
{
  var file = files.next();

  file.setTrashed(true);
//Drive.Files.remove(file.getId());
}

The commented out line also works fine in a non-Team Drive folder.

However, running the Drive.Files.remove line on a Team drive gives this error:

GoogleJsonResponseException: API call to drive.files.delete failed with error: File not found: (file ID here)

Is this a permissions issue (with a misleading error)? Or is there something special I need to do to run Drive.Files.remove on a Team drive? Searching Stack Overflow yielded nothing tangible.

I checked the file ID and it is valid, and the script is running as me and I am a Manager on the Team Drive folder. The Drive API is enabled.

Thanks


Solution

  • Try this:

    Drive.Files.remove(fileId, {supportsTeamDrives:true});
    

    Not sure if this will help but thought it might be worth a try.