Search code examples
pythonpydrive

Pydrive deleting file from Google Drive


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?


Solution

  • 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.