How to delete GCS folder from Python?

Viewed 8095
2 Answers

The code mentioned in the anwser you referred works, the prefix should look like this:

from google.cloud import storage

storage_client = storage.Client()
bucket = storage_client.get_bucket('my-bucket')

blobs = bucket.list_blobs(prefix='my-folder/')

for blob in blobs:
    blob.delete()

from google.cloud import storage
    
def delete_storage_folder(bucket_name, folder):
    """
    This function deletes from GCP Storage

    :param bucket_name: The bucket name in which the file is to be placed
    :param folder: Folder name to be deleted
    :return: returns nothing
    """
    cloud_storage_client = storage.Client()
    bucket = cloud_storage_client.bucket(bucket_name)
    try:
        bucket.delete_blobs(blobs=list(bucket.list_blobs(prefix=folder)))
    except Exception as e:
        print(str(e.message))
Related