2

I wrote a script to upload my models and training examples to Google Drive after every iteration in case of crashes or anything that stops the notebook from running, which looks something like this:

drive_path = 'drive/My Drive/Colab Notebooks/models/'
if path.exists(drive_path):
    shutil.rmtree(drive_path)
shutil.copytree('models', drive_path)

Whenever I check my Google Drive, a few GBs is taken up by dozens of deleted models folder in the Trash, which I have to manually delete them.

The only function in google.colab.drive seems to be mount and that's it.

According to this tutorial, shutil.rmtree() removes a directory permanently but apparently it doesn't work for Drive.

Dlean Jeans
  • 966
  • 1
  • 8
  • 23
  • 4
    Deleting files in Google Drive from Google Colab is not possible. – Matthew Anderson Jul 22 '19 at 18:10
  • A better alternative is to call [shutil.make_archive(drive_path + 'models', 'zip', 'models')](https://docs.python.org/3/library/shutil.html#shutil.make_archive) to zip the models folder which saves over 50% of space and also overrides the old file which is saved as a revision and takes no extra space. – Dlean Jeans Jul 23 '19 at 23:12

3 Answers3

2

It is possible to perform this action inside Google Colab by using the pydrive module. I suggest that you first move your unwanted files and folders to Trash (by ordinarily removing them in your code), and then, anytime you think it's necessary (e.g. you want to free up some space for saving weights of a new DL project), empty your trash by coding the following lines.

In order to permanently empty your Google Drive's Trash, 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 don't want to use my suggestion and want to permanently delete a specific folder in your Drive, it is possible that you have to make more complex queries and deal with fileId, parentId, and the fact that a file or folder in your Drive may have multiple parent folders, when making queries to Google Drive API.

For more information:

  • You can find examples of more complex (yet typical) queries, here.
  • You can find an example of Checking if a file is in a specific folder, here.
  • This statement that Files and folders in Google Drive can each have multiple parent folders may become better and more deeply understood, by reading this post.
0

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
Nickson Yap
  • 1,146
  • 15
  • 23
-1

Just have to move the into trash and connect to your drive. From there delete the notebooks permanently.

Vincenzo
  • 5,304
  • 5
  • 38
  • 96