Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove files on Google Drive using Google Colab

Is there any way to remove files on Google Drive using Google Colab?

I accidentally extracted files from zip on main Google Drive folder using Google Colab but cannot find a way to remove or move them

like image 503
rasyidstat Avatar asked Mar 16 '26 00:03

rasyidstat


1 Answers

I encountered the same issue. I extracted(by mistake) my Dataset(more than 6000 images) to the google drive main folder, which led to many issues such as, every time mounting drive will take longer than usual, sometimes it gives drive-timeout error as it can not list all the files(https://research.google.com/colaboratory/faq.html#drive-timeout). To remove all files, I tried with "os.listdir" but it did not work(no idea why it doesn't). Here is the solution which worked for me:

  1. Create a new Colab
  2. Mount drive using given button(if it doesn't mount, try multiple times)
  3. run the given python script(please change the script according to your file format. The files which I wanted to delete start with "frame" and end with "jpg".
    import os
    import glob
    # my all files starts with "frame" and ends with ".jpg"
    fileList = glob.glob('/content/drive/MyDrive/frame*.jpg')
    print("Number of files: ",len(fileList))
    
    for filePath in fileList:
        try:
            os.remove(filePath)
        except:
            print("Error while deleting file : ", filePath)
like image 179
Rajendra Prajapat Avatar answered Mar 17 '26 12:03

Rajendra Prajapat