Skip to content

Commit

Permalink
Merge pull request #73 from kleyow/force
Browse files Browse the repository at this point in the history
Fix #65 - Added force parameter to delete non empty buckets.
  • Loading branch information
jgeewax committed May 2, 2014
2 parents 4ea692b + 6c999c0 commit fbdff58
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
5 changes: 1 addition & 4 deletions docs/storage-getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,7 @@ otherwise you'll get an error.

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

>>> bucket = connection.get_bucket('my-bucket')
>>> for key in bucket:
... key.delete()
>>> bucket.delete()
>>> bucket = connection.get_bucket('my-bucket', force=True)

Listing available buckets
-------------------------
Expand Down
12 changes: 10 additions & 2 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def new_key(self, key):

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

def delete(self):
def delete(self, force=False):
"""Delete this bucket.
The bucket **must** be empty in order to delete it.
Expand All @@ -139,12 +139,20 @@ def delete(self):
If the bucket is not empty,
this will raise an Exception.
If you want to delete a non-empty bucket you can pass
in a force parameter set to true.
This will iterate through the bucket's keys and delete the related objects,
before deleting the bucket.
:type force: bool
:param full: If True, empties the bucket's objects then deletes it.
:raises: :class:`gcloud.storage.exceptions.NotFoundError`
"""

# TODO: Make sure the proper exceptions are raised.

return self.connection.delete_bucket(self.name)
return self.connection.delete_bucket(self.name, force=force)

def delete_key(self, key):
# TODO: Should we accept a 'silent' param here to not raise an exception?
Expand Down
11 changes: 10 additions & 1 deletion gcloud/storage/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def create_bucket(self, bucket, *args, **kwargs):
data={'name': bucket.name})
return Bucket.from_dict(response, connection=self)

def delete_bucket(self, bucket, *args, **kwargs):
def delete_bucket(self, bucket, force=False, *args, **kwargs):
"""Delete a bucket.
You can use this method to delete a bucket by name,
Expand Down Expand Up @@ -369,12 +369,21 @@ def delete_bucket(self, bucket, *args, **kwargs):
:type bucket: string or :class:`gcloud.storage.bucket.Bucket`
:param bucket: The bucket name (or bucket object) to create.
:type force: bool
:param full: If True, empties the bucket's objects then deletes it.
:rtype: bool
:returns: True if the bucket was deleted.
:raises: :class:`gcloud.storage.exceptions.NotFoundError`
"""

bucket = self.new_bucket(bucket)

# This force delete operation is slow.
if force:
for key in bucket:
key.delete()

response = self.api_request(method='DELETE', path=bucket.path)
return True

Expand Down

0 comments on commit fbdff58

Please sign in to comment.