Update a rule in a ruleset
Last updated Apr 16, 2026 Applies one or more changes to an existing rule in a ruleset at the account or zone level.
Use one of the following API endpoints:
- Update an account ruleset rule
PATCH /accounts/{account_id}/rulesets/{ruleset_id}/rules/{rule_id} - Update a zone ruleset rule
PATCH /zones/{zone_id}/rulesets/{ruleset_id}/rules/{rule_id}
You can update the definition of the rule, changing its fields, or change the order of the rule in the ruleset. Invoking this method creates a new version of the ruleset.
Update the definition of a rule
To update the definition of a rule, include the new rule definition in the request body. You must include all the rule fields that you want to be part of the new rule definition, even if you are not changing their values.
The response will include the complete ruleset after updating the rule.
<details>
<summary>
Required API token permissions
</summary>
At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:
- <code>Mass URL Redirects Write</code>
- <code>Magic Firewall Write</code>
- <code>L4 DDoS Managed Ruleset Write</code>
- <code>Transform Rules Write</code>
- <code>Select Configuration Write</code>
- <code>Account WAF Write</code>
- <code>Account Rulesets Write</code>
- <code>Logs Write</code>
</details>
Update an account ruleset rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets/$RULESET_ID/rules/$RULE_ID_1" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"action": "js_challenge",
"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
"description": "challenge GB and FR based on bot score"
}'
{
"result": {
"id": "<RULESET_ID>",
"name": "Custom Ruleset 1",
"description": "My first custom ruleset",
"kind": "custom",
"version": "11",
"rules": [
{
"id": "<RULE_ID_1>",
"version": "2",
"action": "js_challenge",
"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
"description": "challenge GB and FR based on bot score",
"last_updated": "2023-03-22T12:54:58.144683Z",
"ref": "<RULE_REF_1>",
"enabled": true
},
{
"id": "<RULE_ID_2>",
"version": "1",
"action": "challenge",
"expression": "not http.request.uri.path matches \"^/api/.*$\"",
"last_updated": "2022-11-23T11:36:24.192361Z",
"ref": "<RULE_REF_2>",
"enabled": true
}
],
"last_updated": "2023-03-22T12:54:58.144683Z",
"phase": "http_request_firewall_custom"
},
"success": true,
"errors": [],
"messages": []
}
Change the order of a rule in a ruleset
To reorder a rule in a list of ruleset rules, include a position object in the request, containing one of the following:
"before": "<RULE_ID>"— Places the rule before rule<RULE_ID>. Use this argument with an empty rule ID value ("") to set the rule as the first rule in the ruleset."after": "<RULE_ID>"— Places the rule after rule<RULE_ID>. Use this argument with an empty rule ID value ("") to set the rule as the last rule in the ruleset."index": <POSITION_NUMBER>— Places the rule in the exact position specified by the integer number<POSITION_NUMBER>. Position numbers start with1. Existing rules in the ruleset from the specified position number onward are shifted one position (no rule is overwritten). For example, when you place a rule in positionnusingindex, existing rules with indexn,n+1,n+2, and so on, are shifted one position — their new position will ben+1,n+2,n+3, and so forth. If the index is out of range, the method returns a400HTTP status code.
Important
You can only use one of the before, after, and index fields at a time.
Reorder a rule without changing its definition by including only the position object in the PATCH request body. You can also update a rule definition and reorder it in the same PATCH request by including both the rule object and the position object.
Examples
The following examples build upon the following (abbreviated) ruleset:
{
"rules": [
{ "id": "<RULE_ID_1>" },
{ "id": "<RULE_ID_2>" },
{ "id": "<RULE_ID_3>" },
{ "id": "<RULE_ID_4>" }
]
}
Example 1
The following request with the position object places rule $RULE_ID_2 as the first rule:
<details>
<summary>
Required API token permissions
</summary>
At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:
- <code>Response Compression Write</code>
- <code>Config Settings Write</code>
- <code>Dynamic URL Redirects Write</code>
- <code>Cache Settings Write</code>
- <code>Custom Errors Write</code>
- <code>Origin Write</code>
- <code>Managed headers Write</code>
- <code>Zone Transform Rules Write</code>
- <code>Mass URL Redirects Write</code>
- <code>Magic Firewall Write</code>
- <code>L4 DDoS Managed Ruleset Write</code>
- <code>HTTP DDoS Managed Ruleset Write</code>
- <code>Sanitize Write</code>
- <code>Transform Rules Write</code>
- <code>Select Configuration Write</code>
- <code>Bot Management Write</code>
- <code>Zone WAF Write</code>
- <code>Account WAF Write</code>
- <code>Account Rulesets Write</code>
- <code>Logs Write</code>
- <code>Logs Write</code>
</details>
Update a zone ruleset rulebash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID/rules/$RULE_ID_2" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"position": {
"before": ""
}
}'
In this case, the new rule order would be:
<RULE_ID_2>, <RULE_ID_1>, <RULE_ID_3>, <RULE_ID_4>
Example 2
The following request with the position object places rule $RULE_ID_2 after rule 3:
<details>
<summary>
Required API token permissions
</summary>
At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:
- <code>Response Compression Write</code>
- <code>Config Settings Write</code>
- <code>Dynamic URL Redirects Write</code>
- <code>Cache Settings Write</code>
- <code>Custom Errors Write</code>
- <code>Origin Write</code>
- <code>Managed headers Write</code>
- <code>Zone Transform Rules Write</code>
- <code>Mass URL Redirects Write</code>
- <code>Magic Firewall Write</code>
- <code>L4 DDoS Managed Ruleset Write</code>
- <code>HTTP DDoS Managed Ruleset Write</code>
- <code>Sanitize Write</code>
- <code>Transform Rules Write</code>
- <code>Select Configuration Write</code>
- <code>Bot Management Write</code>
- <code>Zone WAF Write</code>
- <code>Account WAF Write</code>
- <code>Account Rulesets Write</code>
- <code>Logs Write</code>
- <code>Logs Write</code>
</details>
Update a zone ruleset rulebash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID/rules/$RULE_ID_2" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"position": {
"after": "<RULE_ID_3>"
}
}'
In this case, the new rule order would be:
<RULE_ID_1>, <RULE_ID_3>, <RULE_ID_2>, <RULE_ID_4>
Example 3
The following request with the position object places rule $RULE_ID_1 in position 3, becoming the third rule in the ruleset:
<details>
<summary>
Required API token permissions
</summary>
At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:
- <code>Response Compression Write</code>
- <code>Config Settings Write</code>
- <code>Dynamic URL Redirects Write</code>
- <code>Cache Settings Write</code>
- <code>Custom Errors Write</code>
- <code>Origin Write</code>
- <code>Managed headers Write</code>
- <code>Zone Transform Rules Write</code>
- <code>Mass URL Redirects Write</code>
- <code>Magic Firewall Write</code>
- <code>L4 DDoS Managed Ruleset Write</code>
- <code>HTTP DDoS Managed Ruleset Write</code>
- <code>Sanitize Write</code>
- <code>Transform Rules Write</code>
- <code>Select Configuration Write</code>
- <code>Bot Management Write</code>
- <code>Zone WAF Write</code>
- <code>Account WAF Write</code>
- <code>Account Rulesets Write</code>
- <code>Logs Write</code>
- <code>Logs Write</code>
</details>
Update a zone ruleset rulebash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID/rules/$RULE_ID_1" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"position": {
"index": 3
}
}'
In this case, the new rule order would be:
<RULE_ID_2>, <RULE_ID_3>, <RULE_ID_1>, <RULE_ID_4>
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/ruleset-engine/rulesets-api/update-rule/#page","headline":"Update a rule in a ruleset","description":"Update a specific rule in a ruleset using the API.","url":"https://developers.cloudflare.com/ruleset-engine/rulesets-api/update-rule/","inLanguage":"en","image":"https://developers.cloudflare.com/ruleset-engine/rulesets-api/update-rule/og.png?v=ea3b4972b97830df","dateModified":"2026-04-16","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Describes the API operations to list and view the details of rulesets at the account or zone level. title: List and view rulesets image: https://developers.cloudflare.com/ruleset-engine/rulesets-api/view/og.png?v=2f0a7c034219e29e
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/ruleset-engine/llms.txt
Use this file to discover all available pages before exploring further.
Docs