Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #65 - Added force parameter to delete non empty buckets. #73

Merged
merged 1 commit into from
May 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):

This comment was marked as spam.

"""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):

This comment was marked as spam.

"""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:

This comment was marked as spam.

key.delete()

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

Expand Down