Skip to content

Commit fbdff58

Browse files
committed
Merge pull request #73 from kleyow/force
Fix #65 - Added force parameter to delete non empty buckets.
2 parents 4ea692b + 6c999c0 commit fbdff58

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

docs/storage-getting-started.rst

+1-4
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,7 @@ otherwise you'll get an error.
217217

218218
If you have a full bucket, you can delete it this way::
219219

220-
>>> bucket = connection.get_bucket('my-bucket')
221-
>>> for key in bucket:
222-
... key.delete()
223-
>>> bucket.delete()
220+
>>> bucket = connection.get_bucket('my-bucket', force=True)
224221

225222
Listing available buckets
226223
-------------------------

gcloud/storage/bucket.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def new_key(self, key):
130130

131131
raise TypeError('Invalid key: %s' % key)
132132

133-
def delete(self):
133+
def delete(self, force=False):
134134
"""Delete this bucket.
135135
136136
The bucket **must** be empty in order to delete it.
@@ -139,12 +139,20 @@ def delete(self):
139139
If the bucket is not empty,
140140
this will raise an Exception.
141141
142+
If you want to delete a non-empty bucket you can pass
143+
in a force parameter set to true.
144+
This will iterate through the bucket's keys and delete the related objects,
145+
before deleting the bucket.
146+
147+
:type force: bool
148+
:param full: If True, empties the bucket's objects then deletes it.
149+
142150
:raises: :class:`gcloud.storage.exceptions.NotFoundError`
143151
"""
144152

145153
# TODO: Make sure the proper exceptions are raised.
146154

147-
return self.connection.delete_bucket(self.name)
155+
return self.connection.delete_bucket(self.name, force=force)
148156

149157
def delete_key(self, key):
150158
# TODO: Should we accept a 'silent' param here to not raise an exception?

gcloud/storage/connection.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def create_bucket(self, bucket, *args, **kwargs):
340340
data={'name': bucket.name})
341341
return Bucket.from_dict(response, connection=self)
342342

343-
def delete_bucket(self, bucket, *args, **kwargs):
343+
def delete_bucket(self, bucket, force=False, *args, **kwargs):
344344
"""Delete a bucket.
345345
346346
You can use this method to delete a bucket by name,
@@ -369,12 +369,21 @@ def delete_bucket(self, bucket, *args, **kwargs):
369369
:type bucket: string or :class:`gcloud.storage.bucket.Bucket`
370370
:param bucket: The bucket name (or bucket object) to create.
371371
372+
:type force: bool
373+
:param full: If True, empties the bucket's objects then deletes it.
374+
372375
:rtype: bool
373376
:returns: True if the bucket was deleted.
374377
:raises: :class:`gcloud.storage.exceptions.NotFoundError`
375378
"""
376379

377380
bucket = self.new_bucket(bucket)
381+
382+
# This force delete operation is slow.
383+
if force:
384+
for key in bucket:
385+
key.delete()
386+
378387
response = self.api_request(method='DELETE', path=bucket.path)
379388
return True
380389

0 commit comments

Comments
 (0)