Skip to content

Commit

Permalink
fix: bucket.delete(force=True) now works with version-enabled buckets (
Browse files Browse the repository at this point in the history
…#1172)

Fixes #1071 🦕

As a side-effect, the behavior of this method during a race condition has changed slightly. Previously, if a new object was created while the bucket.delete(force=True) method is running, it would fail, but if a new generation of an existing object was uploaded, it would still succeed. Now it will fail in both cases. Regardless of the exact behavior, please do not use this method on a bucket that is still being updated by another process.
  • Loading branch information
andrewsg committed Oct 31, 2023
1 parent 0a243fa commit 0de09d3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,7 @@ def delete(
client=client,
timeout=timeout,
retry=retry,
versions=True,
)
)
if len(blobs) > self._MAX_OBJECTS_FOR_ITERATION:
Expand All @@ -1557,6 +1558,7 @@ def delete(
client=client,
timeout=timeout,
retry=retry,
preserve_generation=True,
)

# We intentionally pass `_target_object=None` since a DELETE
Expand Down
42 changes: 42 additions & 0 deletions tests/system/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,48 @@ def test_new_bucket_with_autoclass(
assert bucket.autoclass_toggle_time != previous_toggle_time


def test_bucket_delete_force(storage_client):
bucket_name = _helpers.unique_name("version-disabled")
bucket_obj = storage_client.bucket(bucket_name)
bucket = storage_client.create_bucket(bucket_obj)

BLOB_NAME = "my_object"
blob = bucket.blob(BLOB_NAME)
blob.upload_from_string("abcd")
blob.upload_from_string("efgh")

blobs = bucket.list_blobs(versions=True)
counter = 0
for blob in blobs:
counter += 1
assert blob.name == BLOB_NAME
assert counter == 1

bucket.delete(force=True) # Will fail with 409 if blobs aren't deleted


def test_bucket_delete_force_works_with_versions(storage_client):
bucket_name = _helpers.unique_name("version-enabled")
bucket_obj = storage_client.bucket(bucket_name)
bucket_obj.versioning_enabled = True
bucket = storage_client.create_bucket(bucket_obj)
assert bucket.versioning_enabled

BLOB_NAME = "my_versioned_object"
blob = bucket.blob(BLOB_NAME)
blob.upload_from_string("abcd")
blob.upload_from_string("efgh")

blobs = bucket.list_blobs(versions=True)
counter = 0
for blob in blobs:
counter += 1
assert blob.name == BLOB_NAME
assert counter == 2

bucket.delete(force=True) # Will fail with 409 if versions aren't deleted


def test_config_autoclass_w_existing_bucket(
storage_client,
buckets_to_delete,
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,7 @@ def test_delete_hit_w_force_w_user_project_w_explicit_timeout_retry(self):
client=client,
timeout=timeout,
retry=retry,
versions=True,
)

bucket.delete_blobs.assert_called_once_with(
Expand All @@ -1427,6 +1428,7 @@ def test_delete_hit_w_force_w_user_project_w_explicit_timeout_retry(self):
client=client,
timeout=timeout,
retry=retry,
preserve_generation=True,
)

expected_query_params = {"userProject": user_project}
Expand Down Expand Up @@ -1456,6 +1458,7 @@ def test_delete_hit_w_force_delete_blobs(self):
client=client,
timeout=self._get_default_timeout(),
retry=DEFAULT_RETRY,
versions=True,
)

bucket.delete_blobs.assert_called_once_with(
Expand All @@ -1464,6 +1467,7 @@ def test_delete_hit_w_force_delete_blobs(self):
client=client,
timeout=self._get_default_timeout(),
retry=DEFAULT_RETRY,
preserve_generation=True,
)

expected_query_params = {}
Expand All @@ -1483,8 +1487,10 @@ def test_delete_w_force_w_user_project_w_miss_on_blob(self):
client = mock.Mock(spec=["_delete_resource"])
client._delete_resource.return_value = None
bucket = self._make_one(client=client, name=name)
blob = mock.Mock(spec=["name"])
blob = mock.Mock(spec=["name", "generation"])
blob.name = blob_name
GEN = 1234
blob.generation = GEN
blobs = [blob]
bucket.list_blobs = mock.Mock(return_value=iter(blobs))
bucket.delete_blob = mock.Mock(side_effect=NotFound("testing"))
Expand All @@ -1496,7 +1502,7 @@ def test_delete_w_force_w_user_project_w_miss_on_blob(self):
bucket.delete_blob.assert_called_once_with(
blob_name,
client=client,
generation=None,
generation=GEN,
if_generation_match=None,
if_generation_not_match=None,
if_metageneration_match=None,
Expand Down

0 comments on commit 0de09d3

Please sign in to comment.