Bars of request counts for plain delete, soft delete and restore

A bucket without versioning has no undo. <code>DeleteObject</code> removes the key and the data, and nothing keeps the old bytes. If you want a safety net anyway, you build it from ordinary requests: copy the object under a <code>trash/</code> prefix, delete the original, and copy it back to restore. We counted exactly what that costs in requests, and what <code>GetBucketVersioning</code> and <code>?versions</code> say about it on tport.space.

What a plain delete leaves

We deleted a key in a bucket with versioning never switched on, then asked for it:

  • DeleteObject: 1 request, HTTP 204.
  • GetObject afterwards: NoSuchKey.
  • GetBucketVersioning: an empty configuration, with no Status field at all.
  • ListObjectVersions for the prefix: no versions.

On tport.space the answers come from the gateway code (checked for this article, not from a live call): <code>GET /?versioning</code> returns 200 with an empty <code>VersioningConfiguration</code> because versioning is never on, and <code>GET /?versions</code> returns 501 <code>NotImplemented</code>, as do lifecycle rules. So there is no delete marker to find, and no rule that empties a trash prefix for you: the purge is a script you run. The rest of this article is that script.

The pattern

python
import datetime

def soft_delete(s3, bucket, key):
    stamp = datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')
    trash = f'trash/{key}/{stamp}'
    s3.copy_object(Bucket=bucket, Key=trash,
                   CopySource={'Bucket': bucket, 'Key': key})   # 1
    s3.delete_object(Bucket=bucket, Key=key)                    # 2
    return trash

def restore(s3, bucket, key):
    pages = s3.get_paginator('list_objects_v2').paginate(Bucket=bucket, Prefix=f'trash/{key}/')
    latest = max(o['Key'] for p in pages for o in p.get('Contents', []))   # 1+
    s3.copy_object(Bucket=bucket, Key=key,
                   CopySource={'Bucket': bucket, 'Key': latest})            # 2
    s3.delete_object(Bucket=bucket, Key=latest)                           # 3

Two choices matter. <b>Copy first, delete second.</b> We simulated a crash between the steps both ways: after copy-then-delete crashed, the original and the trash copy both existed; after a delete-first order crashed, the original was gone and nothing was in the trash. The worst case of the safe order is a duplicate, not a loss. <b>A timestamp in the key</b> lets the same name be deleted twice without the second trash copy overwriting the first (the stamp has one-second resolution, so add a counter if you delete the same key faster than that).

Request counts, measured

We counted requests with a boto3 hook on every call, against a local S3 server running an unversioned bucket (moto 5.2.6, boto3 1.44.13). Counts are the same on any S3 server, because they follow from the API, not the storage.

StepRequestsOperations
Plain delete1DeleteObject
Soft delete2CopyObject, DeleteObject
Soft delete, checking the copy with HeadObject first3CopyObject, HeadObject, DeleteObject
Restore one object3ListObjectsV2, CopyObject, DeleteObject
Restore, refusing to overwrite a newer file at the same key4HeadObject, ListObjectsV2, CopyObject, DeleteObject
Soft-delete a 100-object folder, one delete per object200100 CopyObject, 100 DeleteObject
Soft-delete the same folder, one batched delete1021 ListObjectsV2, 100 CopyObject, 1 DeleteObjects

Soft delete doubles the request count of a delete and a restore costs three. Copies cannot be batched, so a folder of 100 files is at least 100 copies; only the deletes collapse into a <code>DeleteObjects</code> call of up to 1,000 keys. A copy keeps the content type and user metadata (we checked: <code>text/plain</code> and the <code>owner</code> metadata came back, same ETag) but the restored object has a new last-modified time, so anything that sorts by age sees it as new.

Where to put the timestamp: restore against purge

The key layout changes the list cost. We filled the trash with 5,004 entries and compared two layouts: <code>trash/&lt;key&gt;/&lt;stamp&gt;</code> and <code>trash/&lt;stamp&gt;/&lt;key&gt;</code>.

Task (5,004 trashed objects)trash/key/stamptrash/stamp/key
Restore one named object3 requests (1 list of its own prefix)8 requests (6 list pages, because the key is at the end of the name)
Purge entries older than a cutoff (2,504 removed)9 requests (6 list pages, 3 DeleteObjects)6 requests (3 list pages, 3 DeleteObjects; the listing is sorted by time and stops at the cutoff)

Pick by what you do more often. Restores are what people do under pressure, and the key-first layout keeps them at 3 requests however big the trash grows. The time-first layout makes the nightly purge cheaper, and the saving is only 3 list requests at this size. Our choice: key first, and a purge that lists the whole trash once a day.

Big objects and the purge

A single <code>CopyObject</code> works up to 5 GB (5,368,709,120 bytes) on AWS. Beyond that you copy in parts: CreateMultipartUpload, one <code>UploadPartCopy</code> per range, CompleteMultipartUpload. We measured that at small scale: a 15 MiB object in 5 MiB parts took 5 requests (1 + 3 + 1), and boto3's managed <code>s3.copy</code> took 6 because it adds a HeadObject first. For a 12 GiB object with 1 GiB parts that is 1 + 12 + 1 = 14 requests; with 100 MiB parts it is 1 + 123 + 1 = 125. Our gateway lists UploadPartCopy among the operations it supports, but we did not run a copy over 5 GB on tport.space.

The purge is a daily job: list <code>trash/</code>, keep entries newer than the cutoff, delete the rest in batches of 1,000. Retry the keys a <code>DeleteObjects</code> reply marks failed, not the whole batch (<a href="https://tport.space/articles/s3-delete-objects-http-200-partial-failure-retry-failed-keys-only">the 200-with-errors case</a>). Trash entries are real objects: they are stored and counted until the purge removes them, so size the retention to what you can hold.

When to use it, and when not

  • Good for: user-facing apps where "I deleted the wrong file" happens, and for scripts that clean a prefix and you want a few days to notice a mistake.
  • Not a backup. A bug that deletes the trash prefix too, or a key that can delete anything, still wins. Use a separate read-only copy for that; see backup retention in object storage.
  • Restores can overwrite a newer file written under the same key after the delete. The overwrite check costs 1 extra request (the HeadObject row above).
  • Watch the delete permission: the same key does the copy and the delete. A key limited to trash/ for restores would need its own scope.

S3-compatible buckets with no egress fees: copy, delete and list work the way this article counts them.

Explore tport.space

Test record and sources

  • Run on 29 November 2026 on a Mac mini, Python 3.12, boto3 1.44.13, moto 5.2.6 (an S3 server for tests) in-process, bucket without versioning. Requests counted from botocore's before-call hook on every call.
  • Limits: counts are exact for the pattern; they were not taken against a live tport.space bucket. The tport.space answers to ?versioning, ?versions and lifecycle come from reading the gateway code (GetBucketVersioning returns an empty configuration; versions and lifecycle map to NotImplemented). The multipart-copy request counts for large objects are a calculation, tested only at 15 MiB.
  • AWS S3 User Guide: CopyObject (5 GB single-copy limit), copying with multipart upload (UploadPartCopy), DeleteObjects (1,000 keys per call).
  • Scripts and raw results: scripts/bench/s3-soft-delete/ in the content repository.