Salvador Dali
Salvador Dali

Reputation: 222441

How to remove the file from trash in drive in colab

I use a google drive in colab. Basically I do the following:

from google.colab import drive
drive.mount('/content/gdrive')

After this I can use os function (listdir, remove) to manipulate the files. The problem is that after removing the file with os.remove it is not actually removed but goes to trash. I would like to remove a file completely but up till now I have not found how to do this.

I tried to locate the file in a trash but the trash directory shows nothing os.listdir('/content/gdrive/.Trash') also I see the files there in the web interface.

How can I remove the file from trash?

Upvotes: 5

Views: 12382

Answers (5)

Promit Basak
Promit Basak

Reputation: 331

The easiest way to permanently delete files for google drive is the rm command. You can delete a specific file using rm:

!rm /content/drive/MyDrive/somefolder/examplefile.png

Or, you can delete a whole folder:

!rm -r /content/drive/MyDrive/somefolder

Or, you can use wildcards for deleting files with specific patterns:

!rm /content/drive/MyDrive/somefolder/*.png

However, if you want to delete files from trash, you have to use google drive API.

Upvotes: 0

diyism
diyism

Reputation: 12935

We can run these codes in google colab successfully:

from google.colab import drive
drive.mount('/root/gdrive', force_remount=True)

from google.colab import auth
auth.authenticate_user()
from googleapiclient.discovery import build
drive_service = build('drive', 'v3')
drive_service.files().emptyTrash().execute()

but if there're many files in the google drive trash bin, we still need to wait for over 20 minutes, sad.

The rclone command has a immediate effect:

!curl https://rclone.org/install.sh | bash
!rclone config    # to set google drive client id etc

#delete all files in trash and don't put them in trash again:
!rclone --config /root/sing/rclone.conf delete gdrive: --drive-trashed-only --drive-use-trash=false
!rclone --config /root/sing/rclone.conf ls gdrive: --drive-trashed-only --fast-list

Upvotes: 1

Mohammad Ali Dastgheib
Mohammad Ali Dastgheib

Reputation: 109

It is straightforward to perform this action inside Google Colab by using the pydrive module. In order to delete all files from your Google Drive's Trash folder, code the following lines in your Google Colab notebook:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
my_drive = GoogleDrive(gauth)

After entering authentication code and creating a valid instance of GoogleDrive class, write:

for a_file in my_drive.ListFile({'q': "trashed = true"}).GetList():
    # print the name of the file being deleted.
    print(f'the file "{a_file['title']}", is about to get deleted permanently.')
    # delete the file permanently.
    a_file.Delete()

If you'd like to delete a specific file in Trash, then you need to change the last chunck of code. Let's assume you have a file which is named weights-improvement-01-10.5336.hdf5 in your Trash:

for a_file in my_drive.ListFile({'q': "title = 'weights-improvement-01-10.5336.hdf5' and trashed=true"}).GetList():
    # print the name of the file being deleted.
    print(f'the file "{a_file['title']}", is about to get deleted permanently.')    
    # delete the file permanently.
    a_file.Delete()

If you want to make other and perhaps more complex queries, e.g. delete a bunch of files which have the expression weights-improvement- in common in their names, or files all of which have been modified before a given date; visit: 1) Get all files which matches the query, 2) Search for files and folders.

Upvotes: 11

Nickson Yap
Nickson Yap

Reputation: 1276

Jess's answer of using Google Drive API to clear the trash isn't a good way because you might actually have other data in the bin

Because files will move to bin upon delete, so this neat trick reduces the file size to 0 before deleting (cannot be undone!)


import os

delete_filepath = 'drive/My Drive/Colab Notebooks/somefolder/examplefile.png'

open(delete_filename, 'w').close() #overwrite and make the file blank instead - ref: https://stackoverflow.com/a/4914288/3553367
os.remove(delete_filename) #delete the blank file from google drive will move the file to bin instead

Also answered at: https://stackoverflow.com/a/60729089/3553367

Upvotes: 5

Jessica Rodriguez
Jessica Rodriguez

Reputation: 2974

If you're looking for a code for removing the file from the trash, you can check this SO post answered by Tanaike - Empty Google Drive Trash:

def main():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())     
    service = discovery.build('drive', 'v3', http=http)
    service.files().emptyTrash().execute()

or use these methods using Pydrive:

file.Trash() - Move file to trash
file.Untrash() - Move file out of trash
file.Delete() - Permanently delete the file

Upvotes: 2

Related Questions