Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(s3): answer a conditional delete of an absent key 204 (#1451)

Andrew Gaul28550f7db6d3
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index 03471bb1..b1a0830e 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -3721,13 +3721,16 @@ def _delete_object(bucket_name: str, key: str, headers: dict | None = None,
if bucket is None:
return _no_such_bucket(bucket_name)
- # Conditional delete: If-Match against the current object's ETag. A
- # non-matching (or absent) object fails the precondition with 412, so the
- # object is not removed — S3's compare-and-swap delete.
+ # Conditional delete: If-Match against the current object's ETag. An
+ # object whose ETag differs fails the precondition with 412 and stays —
+ # S3's compare-and-swap delete. A key that is not there at all is not a
+ # failed precondition: deleting one is a success either way, and S3
+ # answers the conditional delete of an absent key 204 like any other.
if_match = (headers.get("if-match") or "").strip()
if if_match:
_cur = bucket["objects"].get(key)
- if _cur is None or if_match.strip('"') != _cur["etag"].strip('"'):
+ if _cur is not None and if_match != "*" and (
+ if_match.strip('"') != _cur["etag"].strip('"')):
return _error(
"PreconditionFailed",
"At least one of the preconditions you specified did not hold.",
diff --git a/tests/test_s3.py b/tests/test_s3.py
index eaa1191a..09821128 100644
--- a/tests/test_s3.py
+++ b/tests/test_s3.py
@@ -4982,6 +4982,28 @@ def test_s3_conditional_delete_if_match(s3):
assert s3.get_object(Bucket=bucket, Key="cond")["Body"].read() == b"x"
+def test_s3_conditional_delete_of_absent_key_succeeds(s3):
+ """A conditional delete of a key that is not there is a success, not a
+ failed precondition: S3 counts deleting an absent key as done whatever
+ condition the request carries. If-Match: * means "whatever is there",
+ so it holds against an object of any ETag."""
+ bucket = "intg-s3-conditional-delete-absent"
+ s3.create_bucket(Bucket=bucket)
+
+ for condition in ("*", "badetag"):
+ resp = s3.delete_object(Bucket=bucket, Key="gone", IfMatch=condition)
+ assert resp["ResponseMetadata"]["HTTPStatusCode"] == 204
+
+ s3.put_object(Bucket=bucket, Key="here", Body=b"x")
+ resp = s3.delete_object(Bucket=bucket, Key="here", IfMatch="*")
+ assert resp["ResponseMetadata"]["HTTPStatusCode"] == 204
+ with pytest.raises(ClientError) as exc:
+ s3.head_object(Bucket=bucket, Key="here")
+ assert exc.value.response["Error"]["Code"] == "404"
+
+ s3.delete_bucket(Bucket=bucket)
+
+
def test_s3_copy_object_specific_version(s3):
"""CopyObject with a source ?versionId copies that exact version, not the
current object. Regression for #1328."""