Bucket Lock Rules, Retention Policies, Lifecycle Rules and Bucket Emptying
How bucket lock rules protect objects from deletion, interact with lifecycle and empty-bucket steps, and how to configure them in dashboard, Wrangler, and API.
This page summarizes Cloudflare R2 documentation on bucket lock rules (retention policies), how they interact with object lifecycles and delete buckets workflows, and what blocks deletion in the dashboard and APIs.
Bucket lock rules: what retention does on object storage
Section titled “Bucket lock rules: what retention does on object storage”Bucket lock rules prevent deletion and overwriting of objects in a bucket for a set period—or indefinitely. They enforce retention on objects so data is not removed accidentally or too early.
Bucket locks prevent the deletion and overwriting of objects in an R2 bucket for a specified period — or indefinitely. When enabled, bucket locks enforce retention policies on your objects, helping protect them from accidental or premature deletions.
— Cloudflare, Bucket locks
Bucket locks do not stop new uploads. They protect objects that already exist and objects created after a rule is enabled. Once a rule update succeeds, protected objects cannot be overwritten, deleted, removed by lifecycle expiration, or cleared by an in-progress upload completion—including multipart uploads that started before the lock applied.
Retention rule types: duration, date, indefinite, and prefix scope
Section titled “Retention rule types: duration, date, indefinite, and prefix scope”A bucket lock configuration can include up to 1,000 rules. Each rule names which objects it covers (via prefix) and how long they must stay locked.
You can:
- Lock objects for a specific duration (for example 90 days).
- Retain until a date (for example until January 1, 2026).
- Keep objects locked indefinitely until the rule is removed.
If multiple rules apply to the same prefix or object key, the strictest (longest) retention requirement takes precedence.
— Cloudflare, Bucket locks
Rules without a prefix apply to all objects in the bucket. For compliance-style setups, Set retention polices for your R2 bucket with bucket locks shows typical Wrangler examples: 180-day retention for the whole bucket, or indefinite retention under logs/.
| Condition type | Meaning | Example |
|---|---|---|
Age (maxAgeSeconds) |
Minimum retention from object creation/upload | 7 days = 604800 seconds |
| Until date | Retain until calendar date | retention-date in Wrangler |
| Indefinite | No automatic unlock | --retention-indefinite |
How to add bucket lock rules: dashboard, Wrangler, and API
Section titled “How to add bucket lock rules: dashboard, Wrangler, and API”Bucket lock rules in the Cloudflare dashboard
Section titled “Bucket lock rules in the Cloudflare dashboard”In the R2 object storage area, open the bucket → Settings → Bucket lock rules → Add rule. Enter the rule name, prefix, and retention period, then save. The same card is used to list and delete rules (Bucket locks).
r2 bucket lock Wrangler commands
Section titled “r2 bucket lock Wrangler commands”After wrangler login, add a rule:
npx wrangler r2 bucket lock add <BUCKET_NAME> [OPTIONS]Set the full configuration from JSON (same shape as the PUT lock API):
npx wrangler r2 bucket lock set <BUCKET_NAME> --file <FILE_PATH>Examples from the changelog:
npx wrangler r2 bucket lock add <bucket> --name 180-days-all --retention-days 180npx wrangler r2 bucket lock add <bucket> --name indefinite-logs --prefix logs/ --retention-indefiniteList or remove rules:
npx wrangler r2 bucket lock list <BUCKET_NAME>npx wrangler r2 bucket lock remove <BUCKET_NAME> --id <RULE_ID>Command flags are documented under Wrangler commands and R2 Wrangler reference.
PUT bucket lock configuration API
Section titled “PUT bucket lock configuration API”Replace the entire rule set with a JSON body on:
PUT https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/r2/buckets/<BUCKET_NAME>/lock
Bucket locks documents a two-rule example: logs/ locked for 7 days (Age / maxAgeSeconds: 604800) and images/ locked with Indefinite. To remove rules via API, send an updated configuration without those rule IDs (same PUT endpoint).
Bucket lock vs object lifecycle: which rule wins when deleting objects
Section titled “Bucket lock vs object lifecycle: which rule wins when deleting objects”Object lifecycle rules expire or transition objects on a schedule—for example delete after 90 days or move to Infrequent Access after 30 days (Object lifecycles). Lifecycle is useful for routine cleanup; bucket locks are for minimum retention and compliance.
Bucket lock rules take precedence over lifecycle rules. For example, if a lifecycle rule attempts to delete an object at 30 days but a bucket lock rule requires it be retained for 90 days, the object will not be deleted until the 90-day requirement is met.
— Cloudflare, Bucket locks
Lifecycle deletion is typically applied within about 24 hours of the expiration condition, but large buckets can take longer (Object lifecycles). Even when lifecycle would delete an object, a lock blocks the delete until retention ends.
For emptying a bucket quickly via lifecycle, Delete buckets suggests a short expiration (for example 1 day) with Wrangler:
npx wrangler r2 bucket lifecycle add <BUCKET_NAME> --expire-days 1That only works for unlocked objects. Remove lifecycle rules after the bucket is empty so new uploads are not expired unintentionally.
Empty bucket and delete bucket: when locks block removal
Section titled “Empty bucket and delete bucket: when locks block removal”You cannot empty a bucket while any bucket lock rules remain. Remove all lock rules first, then delete objects (Delete buckets, Bucket locks).
A bucket cannot be emptied while any bucket lock rules are configured. Remove all lock rules before emptying a bucket. Bucket lock rules also apply when deleting folders from the dashboard.
— Cloudflare, Bucket locks
Empty bucket (dashboard Settings → Empty Bucket) deletes all objects but keeps bucket configuration (Empty buckets and delete folders from the R2 dashboard). Delete bucket requires an empty bucket first; otherwise the API returns an error such as the bucket not being empty (Consistency model, Delete buckets).
Folders in the dashboard are prefix groupings, not real directories (Objects). Deleting a folder removes every object under that prefix (Delete objects)—subject to locks on those keys.
Programmatic bulk delete (list + delete by age) appears in Backup and restore for sandbox backups; that pattern only succeeds for objects not protected by a lock rule.
Delete and overwrite errors: HTTP 409 and ObjectLockedByBucketPolicy
Section titled “Delete and overwrite errors: HTTP 409 and ObjectLockedByBucketPolicy”When a lock blocks an S3-compatible request:
- A single-object delete or overwrite fails with HTTP 409 and error code
ObjectLockedByBucketPolicy. DeleteObjectsmay return HTTP 200 withObjectLockedByBucketPolicyper locked key in the response body.
The lock check runs before conditions on the object being written, such as If-None-Match or If-Match, so a write that fails both checks returns ObjectLockedByBucketPolicy, not PreconditionFailed (412). In either case the existing object is not modified.
— Cloudflare, Bucket locks
In the dashboard, delete actions on locked objects are disabled so objects do not appear deleted and reappear after refresh (Release notes, 2025-09-23).
Common mistakes with bucket locks, lifecycle, and empty bucket
Section titled “Common mistakes with bucket locks, lifecycle, and empty bucket”- Trying to empty before removing locks — Empty fails until every lock rule is deleted.
- Expecting lifecycle to purge locked data early — Longer retention from bucket lock always wins over shorter lifecycle expiration.
- Overlapping prefixes — Multiple rules on the same keys use the strictest retention; plan prefixes to avoid surprise minimum retain periods.
- Indefinite
logs/lock plus cleanup scripts — Workers or S3 delete loops fail withObjectLockedByBucketPolicyuntil the rule is changed or removed. - Using lifecycle “expire in 1 day” to delete a bucket — New uploads during the wait window are also expired; remove the lifecycle rule as soon as the bucket is empty (Delete buckets).
- Jurisdictional buckets — Lock API needs the correct
cf-r2-jurisdictionheader; Local uploads is not supported on jurisdictional buckets (separate from locks, but affects upload architecture).
Observability: metrics and Data Access Logs after lock changes
Section titled “Observability: metrics and Data Access Logs after lock changes”To confirm who deleted or attempted to delete objects, enable R2 Data Access Logs on non-jurisdictional buckets. Logs cover read, write, list, multipart, and delete operations with status codes below 400, delivered asynchronously to Workers Observability (R2 Data Access Logs changelog). Delivery is best effort—not a complete audit trail.
Per-bucket Metrics and account-level bandwidth views help correlate upload/download patterns (Metrics and analytics, R2 bandwidth usage metrics).
How to check this on your site
Section titled “How to check this on your site”- In the Cloudflare dashboard, open your bucket → Settings → Bucket lock rules and confirm prefixes and retention match policy (Bucket locks).
- Run
npx wrangler r2 bucket lock list <BUCKET_NAME>and compare with dashboard rules (Wrangler commands). - Attempt delete on a known locked test object via S3 API or dashboard; expect 409 /
ObjectLockedByBucketPolicyor a disabled delete control (Bucket locks, Release notes). - Review Object Lifecycle Rules on the same bucket and verify expiration ages are not shorter than lock retention where both apply (Object lifecycles).
- You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page does not match your bucket behavior or the official docs, email yimasynthetic@gmail.com with the page URL and the exact statement you think is wrong or unclear.