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.
I could understand like above. In order to achieve this, how about this modification?
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()
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With