Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pydrive deleting file from Google Drive

Tags:

python

pydrive

I´m writing a small script in python 3.5.2 with pydrive 1.2.1 and I need to be able to delete a file which is not stored locally in my computer, but in my account of Google Drive. The docs only show how to delete a file you previously created, not one which is already stored on the drive. Is deleting an existing file actually possible with pydrive? if so, how?

like image 779
TobiasP Avatar asked Sep 10 '16 19:09

TobiasP


People also ask

Can you permanently delete files from Google Drive?

You can permanently delete an individual file or empty your entire trash. After you delete a file permanently, anyone you've shared the file will lose access to it. If you want others to be able to view the file, you can transfer ownership to someone else.

Why files are not deleting from Google Drive?

Open your Google Drive account and check for the files in My Drive and Shared Drive. If the files are located in Shared Drive then they cannot be deleted. Else if the file is in My Drive then try using some other browse or clear cache and try again.

Can editors remove files on Google Drive?

Show activity on this post. Editors in a shared folder in Google Drive can do these things: They can move these files, even out of the shared folder.


1 Answers

From the docs seems that drive.CreateFile() create only a reference to a file, this can be local or remote. As you can se here drive.CreateFile() is used to download a remote file.

I believe that something like this should do the job:

# Initialize GoogleDriveFile instance with file id.
file1 = drive.CreateFile({'id': <file-id>})

file1.Trash()  # Move file to trash.
file1.UnTrash()  # Move file out of trash.
file1.Delete()  # Permanently delete the file.
like image 189
Ceppo93 Avatar answered Sep 23 '22 18:09

Ceppo93