Google Drive API Delete Python

Viewed 5327

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.

1 Answers

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()
Related