Bucket locks
Model-written
contextmethodsetupresultanalysislimitation
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.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.Bucket locks prevent object deletion and overwriting for a set period or indefinitely Enforced retention policies protect objects from accidental or premature deletionBucket locks prevent object deletion and overwriting for a set period or indefinitelyBucket locks
Get started with bucket locks
You need an existing bucket and, for API, an API token
Before getting started, you will need:Before getting started, you will need:Getting started requires prerequisites firstGetting started requires prerequisites firstprerequisites
- An existing R2 bucket. If you do not already have an existing R2 bucket, refer to Create buckets.An existing R2 bucket.An existing R2 bucket is required Refer to Create buckets if you do not already have oneAn existing R2 bucket is requiredR2 bucket
- (API only) An API token with permissions to edit R2 bucket configuration.(API only) An API token with permissions to edit R2 bucket configuration.API users need a token with R2 bucket configuration edit permissionsAPI users need a token with R2 bucket configuration edit permissionsAPI token
Enable bucket lock via dashboard
Add a lock rule in the dashboard Settings tab and save
-
In the Cloudflare dashboard, go to the R2 object storage page.
-
Select the bucket you would like to add bucket lock rule to.
-
Switch to the Settings tab, then scroll down to the Bucket lock rules card.
-
Select Add rule and enter the rule name, prefix, and retention period.
-
Select Save changes.
Enable bucket lock via Wrangler
Use r2 bucket lock add or set a JSON configuration file
- Install
npm. - Install Wrangler, the Developer Platform CLI.
- Log in to Wrangler with the
wrangler logincommand. - Add a bucket lock rule to your bucket by running the
r2 bucket lock addcommand.Add a bucket lock rule to your bucket by running ther2 bucket lock addcommand.Add a lock rule with r2 bucket lock addAdd a lock rule with r2 bucket lock addlock add
npx wrangler r2 bucket lock add <BUCKET_NAME> [OPTIONS]Alternatively, you can set the entire bucket lock configuration for a bucket from a JSON file using the r2 bucket lock set command.Alternatively, you can set the entire bucket lock configuration for a bucket from a JSON file using the r2 bucket lock set command.Set the whole lock configuration from a JSON file with r2 bucket lock setSet the whole lock configuration from a JSON file with r2 bucket lock setlock set
npx wrangler r2 bucket lock set <BUCKET_NAME> --file <FILE_PATH>The JSON file should be in the format of the request body of the put bucket lock configuration API.The JSON file should be in the format of the request body of the put bucket lock configuration API.The JSON file must match the put bucket lock configuration request bodyThe JSON file must match the put bucket lock configuration request bodyJSON format
Enable bucket lock via API
PUT the bucket lock endpoint with a rules array
For information about getting started with the Cloudflare API, refer to Make API calls. For information on required parameters and more examples of how to set bucket lock configuration, refer to the API documentation.For information about getting started with the Cloudflare API, refer to Make API calls. For information on required parameters and more examples of how to set bucket lock configuration, refer to the API documentation.API getting started and parameters are covered in linked docsAPI getting started and parameters are covered in linked docsAPI docs
Below is an example of setting a bucket lock configuration (a collection of rules):Below is an example of setting a bucket lock configuration (a collection of rules):A lock configuration is a collection of rules set via PUTA lock configuration is a collection of rules set via PUTconfiguration
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/r2/buckets/<BUCKET_NAME>/lock" \
-H "Authorization: Bearer <API_TOKEN>" \
-H "Content-Type: application/json" \
-d '{
"rules": [
{
"id": "lock-logs-7d",
"enabled": true,
"prefix": "logs/",
"condition": {
"type": "Age",
"maxAgeSeconds": 604800
}
},
{
"id": "lock-images-indefinite",
"enabled": true,
"prefix": "images/",
"condition": {
"type": "Indefinite"
}
}
]
}'This request creates two rules:This request creates two rules:The request creates two rulesThe request creates two rulestwo rules
lock-logs-7d: Objects under thelogs/prefix are retained for 7 days (604800 seconds).lock-logs-7d: Objects under thelogs/prefix are retained for 7 days (604800 seconds).lock-logs-7d retains logs/ objects for 7 days 604800 seconds equals seven dayslock-logs-7d retains logs/ objects for 7 dayslock-logs-7dlock-images-indefinite: Objects under theimages/prefix are locked indefinitely.lock-images-indefinite: Objects under theimages/prefix are locked indefinitely.lock-images-indefinite locks images/ objects indefinitelylock-images-indefinite locks images/ objects indefinitelylock-images-indefinite
Note
If your bucket is setup with jurisdictional restrictions, you will need to pass a cf-r2-jurisdiction request header with that jurisdiction. For example, cf-r2-jurisdiction: eu.If your bucket is setup with jurisdictional restrictions, you will need to pass a cf-r2-jurisdiction request header with that jurisdiction. For example, cf-r2-jurisdiction: eu.Jurisdictional buckets require a cf-r2-jurisdiction request header For example, pass cf-r2-jurisdiction: eu for the EU jurisdictionJurisdictional buckets require a cf-r2-jurisdiction request headercf-r2-jurisdiction
Wrangler
List rules with r2 bucket lock list
To list bucket lock rules, run the r2 bucket lock list command:To list bucket lock rules, run the r2 bucket lock list command:List lock rules with r2 bucket lock listList lock rules with r2 bucket lock listlock list
npx wrangler r2 bucket lock list <BUCKET_NAME>API
Get rules via the API documentation endpoint
For more information on required parameters and examples of how to get bucket lock rules, refer to the API documentation.For more information on required parameters and examples of how to get bucket lock rules, refer to the API documentation.Getting lock rules via API is documented in the API referenceGetting lock rules via API is documented in the API referenceAPI reference
Dashboard
Delete a rule from the Settings tab via the ... icon
-
In the Cloudflare dashboard, go to the R2 object storage page.
-
Select the bucket you would like to add bucket lock rule to.
-
Switch to the Settings tab, then scroll down to the Bucket lock rules card.
Locate the rule you want to remove, select the
...icon next to it, and then select Delete.Locate the rule you want to remove, select the...icon next to it, and then select Delete.Delete a rule via the ... icon next to itDelete a rule via the ... icon next to it... icon
Wrangler
Remove a rule with r2 bucket lock remove and its id
To remove a bucket lock rule, run the r2 bucket lock remove command:To remove a bucket lock rule, run the r2 bucket lock remove command:Remove a rule with r2 bucket lock remove and its idRemove a rule with r2 bucket lock remove and its idlock remove
npx wrangler r2 bucket lock remove <BUCKET_NAME> --id <RULE_ID>API
Exclude rules from the updated configuration to remove them
To remove bucket lock rules via API, exclude them from your updated configuration and use the put bucket lock configuration API.To remove bucket lock rules via API, exclude them from your updated configuration and use the put bucket lock configuration API.To remove rules via API, exclude them from the updated configuration The put bucket lock configuration API replaces rules absent from the new configurationTo remove rules via API, exclude them from the updated configurationupdated configuration
Bucket lock rules
Up to 1,000 rules; strictest retention wins
A bucket lock configuration can include up to 1,000 rules. Each rule specifies which objects it covers (via prefix) and how long those objects must remain locked. You can:A bucket lock configuration can include up to 1,000 rules. Each rule specifies which objects it covers (via prefix) and how long those objects must remain locked.A configuration holds up to 1,000 rules, each with prefix and durationA configuration holds up to 1,000 rules, each with prefix and duration1,000 rules
- Lock objects for a specific duration. For example, 90 days.Lock objects for a specific duration.Rules can lock objects for a specific duration For example, 90 daysRules can lock objects for a specific durationspecific duration
- Retain objects until a certain date. For example, until January 1, 2026.Retain objects until a certain date.Rules can retain objects until a certain date For example, until January 1, 2026Rules can retain objects until a certain datecertain date
- Keep objects locked indefinitely.Keep objects locked indefinitely.Rules can keep objects locked indefinitelyRules can keep objects locked indefinitelylocked indefinitely
If multiple rules apply to the same prefix or object key, the strictest (longest) retention requirement takes precedence.If multiple rules apply to the same prefix or object key, the strictest (longest) retention requirement takes precedence.The strictest longest retention requirement takes precedence on overlap This resolves conflicts when multiple rules apply to the same prefix or keyThe strictest longest retention requirement takes precedence on overlapstrictest retention
Notes
Locks override lifecycle rules and block bucket emptying
- Rules without prefix apply to all objects in the bucket.Rules without prefix apply to all objects in the bucket.Rules without a prefix apply to all bucket objectsRules without a prefix apply to all bucket objectsall objects
- Rules apply to both new and existing objects in the bucket.Rules apply to both new and existing objects in the bucket.Rules apply to both new and existing objectsRules apply to both new and existing objectsexisting objects
- 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.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.Bucket lock rules take precedence over lifecycle rules An object due at 30 days stays until the 90-day lock is metBucket lock rules take precedence over lifecycle ruleslifecycle rules
- 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.A bucket cannot be emptied while any bucket lock rules are configured. Remove all lock rules before emptying a bucket.A bucket cannot be emptied while lock rules exist Rules also apply when deleting folders from the dashboardA bucket cannot be emptied while lock rules existemptying a bucket