0

I am having a temporary folder which I want to delete in gcp bucket I want to delete it with all its content, what I thought of is I can pass the path of this temp folder as a prefix and list all blobs inside it and delete every blob, but I had doubts that it would delete the blobs inside it without deleting the folder itself, is it right? The aim goal is to find folder1/ empty without the temp folder but, when I tried I found un expected behavior I found the folder that contains this temp folder is deleted !! for example if we have folder1/tempfolder/file1.csv and folder1/tempfolder/file2.csv I found that folder1 is deleted after applying my changes, here are the changes applied:

delete_folder_on_uri(storage_client,"gs://bucketname/folder1/tempfolder/")
def delete_folder_on_uri(storage_client, folder_gcs_uri):
    bucket_uri = BucketUri(folder_gcs_uri)
    delete_folder(storage_client, bucket_uri.name, bucket_uri.key)

def delete_folder(storage_client, bucket_name, folder):
    bucket = storage_client.get_bucket(bucket_name)
    blobs = bucket.list_blobs(prefix=folder)

    for blob in blobs:
        
        blob.delete()

PS: BucketUri is a class in which bucket_uri.name retrieves the bucket name and bucket_uri.key retrieves the path which is folder1/tempfolder/

Mee
  • 1,413
  • 5
  • 24
  • 40

1 Answers1

1

There is no such thing as folder in the GCS at all. The "folder" - is just a human friendly representation of an object's (or file's) prefix. So the full path looks like gs://bucket-name/prefix/another-prefix/object-name. As there are no folders (ontologically) - thus - there is nothing to delete.

Thus you might need to delete all objects (files) which start with a some prefix in a given bucket.

I think you are doing everything correctly.

Here is an old example (similar to your code) - How to delete GCS folder from Python?

And here is an API description - Blobs / Objects - you might like to check the delete method.

Pit
  • 736
  • 3
  • 17
al-dann
  • 2,545
  • 1
  • 12
  • 22
  • Thanks for your answer, yea that's why I expected that I will find the objects deleted only and the containers (the prefix exist) but in my case I don't find it, it deletes the object with the folder (prefix) contains it. that folder1/tempfolder/ doesn't exist anymore not only the files in it. – Mee Jan 19 '22 at 13:33