4

The Google Drive Rest API v3 has a Drive.Files.Delete method, but that permanently deletes the file.How do i move the file to the trash?

I looked at the docs for updating file metadata, and i tried to do this, but it doesn't seem to work:

File file = new File();
file.setTrashed(true);
driveService.files().update(f.getId(), file).execute();
cgifox
  • 639
  • 11
  • 23
  • I was using service account to delete file, and then looking trash of admin user)))) of course it wasn't there, because file was going to service account's trash – 9900kf Jun 20 '23 at 05:44

1 Answers1

9

I don't see any error with your code on how to move a file to trash wherein you'll use files.update with {'trashed':true}.

Sample code in this thread:

The solution is to create an empty File setting only the new values:

File newContent = new File();
newContent.setTrashed(true);
service.files().update(fileId, newContent).execute();

I have tried this in Google Drive Try it! and successfully moved the file to trash.

Just make sure that the File refers to com.google.api.services.drive.model.File and it is not java.io.File.

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59