Skip to content

Commit 2790a36

Browse files
committed
support deleting a specific blob generation
1 parent b8b28b1 commit 2790a36

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

storage/google/cloud/storage/blob.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def exists(self, client=None):
388388
except NotFound:
389389
return False
390390

391-
def delete(self, client=None):
391+
def delete(self, client=None, generation=None):
392392
"""Deletes a blob from Cloud Storage.
393393
394394
If :attr:`user_project` is set on the bucket, bills the API request
@@ -405,7 +405,7 @@ def delete(self, client=None):
405405
(propagated from
406406
:meth:`google.cloud.storage.bucket.Bucket.delete_blob`).
407407
"""
408-
return self.bucket.delete_blob(self.name, client=client)
408+
return self.bucket.delete_blob(self.name, client=client, generation=generation)
409409

410410
def _get_transport(self, client):
411411
"""Return the client's transport.

storage/google/cloud/storage/bucket.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ def delete(self, force=False, client=None):
575575
query_params=query_params,
576576
_target_object=None)
577577

578-
def delete_blob(self, blob_name, client=None):
578+
def delete_blob(self, blob_name, client=None, generation=None):
579579
"""Deletes a blob from the current bucket.
580580
581581
If the blob isn't found (backend 404), raises a
@@ -597,6 +597,11 @@ def delete_blob(self, blob_name, client=None):
597597
:param client: Optional. The client to use. If not passed, falls back
598598
to the ``client`` stored on the current bucket.
599599
600+
:type generation: :class:`~google.cloud.storage.client.Client` or
601+
``NoneType``
602+
:param generation: Optional. The generation of the blob to delete. If
603+
not passed, deletes the current version.
604+
600605
:raises: :class:`google.cloud.exceptions.NotFound` (to suppress
601606
the exception, call ``delete_blobs``, passing a no-op
602607
``on_error`` callback, e.g.:
@@ -616,11 +621,18 @@ def delete_blob(self, blob_name, client=None):
616621
# We intentionally pass `_target_object=None` since a DELETE
617622
# request has no response value (whether in a standard request or
618623
# in a batch request).
619-
client._connection.api_request(
620-
method='DELETE',
621-
path=blob_path,
622-
query_params=query_params,
623-
_target_object=None)
624+
if not generation:
625+
client._connection.api_request(
626+
method='DELETE',
627+
path=blob_path,
628+
query_params=query_params,
629+
_target_object=None)
630+
else:
631+
client._connection.api_request(
632+
method='DELETE',
633+
path=blob_path + '?generation={}'.format(generation),
634+
query_params=query_params,
635+
_target_object=None)
624636

625637
def delete_blobs(self, blobs, on_error=None, client=None):
626638
"""Deletes a list of blobs from the current bucket.

0 commit comments

Comments
 (0)