Reputation: 15982
How do I delete buckets in a batch?
Here's what i've tried.
def deleteAllBuckets():
batch = storage_client.batch()
with batch:
for bucket in storage_client.list_buckets():
bucket.delete()
Technically it works because the buckets get deleted, however i'm not convinced i'm sending a single batch request. It looks like i'm sending one request per bucket.
Compare the above code to a batch request in Google Cloud Datastore
def deleteAllEntities():
query = datastore_client.query(kind="Charge")
queryIter = query.fetch()
batch = datastore_client.batch()
with batch:
for entity in queryIter:
batch.delete(entity.key)
You can see that i'm calling a delete method on the batch object. With the storage code, i'm calling delete on the bucket object. Unfortunately the cloud storage python API doesn't have any examples.
Upvotes: 6
Views: 348
Reputation: 67073
When you use the context manager, it automatically adds the request to the batch and it gets executed when exiting the context manager.
However, the problem I see with your code is that you're calling list buckets inside the batch context manager. I think you want to create the bucket iterator outside. Something like this:
def deleteAllBuckets():
buckets_iterator = storage_client.list_buckets()
batch = storage_client.batch()
with batch:
for bucket in buckets_iterator:
bucket.delete()
Upvotes: 2