White Phantom
White Phantom

Reputation: 141

Google Drive API Delete Python

How do you delete a file in your google drive with the Google Drive API? I’m using v3 and I’ve literally tried everything and nothing’s been working. I believe the ones I’ve tried to be outdated, what is the current way?

Update: I wasn’t really experimenting with a code, just looking for a Method which is why I didn’t show. Nevertheless,

 service.files().delete(fileId=fileid).execute()

The error I’m getting is with service, that it has no attribute files. I’ve defined everything else (service, file id), so I’ve drawn the conclusion that it’s out of date.

Sorry for any errors btw, I’m on a mobile.

Upvotes: 5

Views: 9841

Answers (2)

Simone jovenitti
Simone jovenitti

Reputation: 31

To avoid permission denied, the following code is OK for trashing the file instead of removing it:

_ = service.files().update(fileId=fileDelete, body={'trashed': True}).execute()

and if you are in a shared drive, remember to use:

_ = service.files().update(fileId=fileDelete, body={'trashed': True}, supportsAllDrives=True ).execute()

Upvotes: 3

this code is ok. for me:

from __future__ import print_function
import os
from apiclient.http import MediaFileUpload
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/drive'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
    creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))

fileDelete='1AKMgCR6v-6uc-JSvhsttBITJzf7k-pDg'
file = DRIVE.files().delete(fileId=fileDelete).execute()

Upvotes: 7

Related Questions