From 6c999c020dca61b76452355c6775778113570ca8 Mon Sep 17 00:00:00 2001 From: Kevin Leyow Date: Fri, 25 Apr 2014 18:20:51 -0500 Subject: [PATCH] Fix #65 - Added force parameter to delete non empty buckets. Updated gcloud python docs to reflect forced bucket deletes. --- docs/storage-getting-started.rst | 5 +---- gcloud/storage/bucket.py | 12 ++++++++++-- gcloud/storage/connection.py | 11 ++++++++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/docs/storage-getting-started.rst b/docs/storage-getting-started.rst index 63e1266b5a2f..3cca86d0d5cd 100644 --- a/docs/storage-getting-started.rst +++ b/docs/storage-getting-started.rst @@ -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 ------------------------- diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index 6c3cfe07ff00..cc0d89bf76d3 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -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. @@ -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? diff --git a/gcloud/storage/connection.py b/gcloud/storage/connection.py index ec25a843f181..9901fac34f04 100644 --- a/gcloud/storage/connection.py +++ b/gcloud/storage/connection.py @@ -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, @@ -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