3

I need to trash a file by using the Google Drive API V3. While V2 has a trash method, V3 doesn't have one. Instead, you need to modify the file's metadata.

I've searched everywhere but couldn't find an answer for this. I've found an example for Android here in stackoverflow, but I don't know how to translate it into python: Google Drive Rest API v3 - how to move a file to the trash?.

Here's the function that's supposed to trash the files:

def trashFile(service, file_id):
        # First retrieve the file from the API.
        file = service.files().get(fileId=file_id,fields="trashed" ).execute()

        # File's new metadata.
        file['trashed'] = True

        # Send the request to the API.
        updated_file = service.files().update(body=file).execute()
        return updated_file
        return None

Everything runs smoothly, the file is retrieved, the file ID is correct, the update command is executed, but the file is not trashed.

What should I do? Thanks in advance for your help.

  • Possible duplicate of [Google Drive Rest API v3 - how to move a file to the trash?](https://stackoverflow.com/questions/40286246/google-drive-rest-api-v3-how-to-move-a-file-to-the-trash) – Smart Manoj Jun 05 '19 at 15:26
  • Did you try it on https://developers.google.com/drive/api/v3/reference/files/update#try-it – Smart Manoj Jun 05 '19 at 15:30
  • @SmartManoj yes, I checked that, and there you will find the "trashed" property that I am trying to update. And it's not a duplicate because the other question is about Android and I don't know how to translate that into Python. – Clement Bjelland Jun 05 '19 at 15:38
  • Did you try that online api – Smart Manoj Jun 05 '19 at 15:40
  • @Clement Bjelland Can I ask you about your question? You want to move a file to the trash. Or you want to directly delete a file using Drive API v3. Which do you want to do using Drive API v3? – Tanaike Jun 05 '19 at 22:45
  • @Tanaike I would like to move it to the trash, not delete it permanently. – Clement Bjelland Jun 06 '19 at 01:24
  • @Clement Bjelland Thank you for replying. I proposed a modified script as an answer. Could you please confirm it? If I misunderstood your question and this was not the result you want, I apologize. – Tanaike Jun 06 '19 at 02:24

1 Answers1

4
  • You want to move a file to the trash using Drive API v3 by the Google Client Library with Python.

I could understand like above. In order to achieve this, how about this modification?

Modified script 1:

If your script is modified, please modify as follows.

From:
updated_file = service.files().update(body=file).execute()
To:
updated_file = service.files().update(fileId=file_id, body=file).execute()

Modified script 2:

For example, how about this modified script? In this case, it can be achieved by one API call.

def trashFile(service, file_id):
    body = {'trashed': True}
    updated_file = service.files().update(fileId=file_id, body=body).execute()
    return updated_file

Note:

  • If the file you want to move the trash is NOT your file, the file might not be able to be moved. Please be careful this.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165