|
| 1 | +--- |
| 2 | +title: Purge zone versions via API |
| 3 | +pcx_content_type: how-to |
| 4 | +sidebar: |
| 5 | + order: 8 |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +To purge zone versions via the Cloudflare API, follow these steps: |
| 10 | + |
| 11 | +## Step 1: Retrieve the environment ID |
| 12 | + |
| 13 | +First, retrieve your zone's environment ID using the following API request: |
| 14 | + |
| 15 | +```bash |
| 16 | +https://api.cloudflare.com/client/v4/zones/<zone-tag>/environments | jq . |
| 17 | +``` |
| 18 | + |
| 19 | +This API call will return a JSON response similar to the example below: |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "result": { |
| 24 | + "environments": [ |
| 25 | + { |
| 26 | + "name": "Production", |
| 27 | + "ref": "40bebe5d89b34840a573f51834a54006", |
| 28 | + "version": 0, |
| 29 | + "expression": "(cf.zone.name eq \"jfowler.theburritobot.com\")", |
| 30 | + "locked_on_deployment": false, |
| 31 | + "position": { |
| 32 | + "before": "53278ccfd7564fa69172f19b43c8b8a9" |
| 33 | + } |
| 34 | + }, |
| 35 | + { |
| 36 | + "name": "Staging", |
| 37 | + "ref": "53278ccfd7564fa69172f19b43c8b8a9", |
| 38 | + "version": 0, |
| 39 | + "expression": "((cf.edge.server_ip in {162.159.133.39 162.159.134.39})) and (cf.zone.name eq \"jfowler.theburritobot.com\")", |
| 40 | + "locked_on_deployment": false, |
| 41 | + "position": { |
| 42 | + "before": "2f85286bafdf439f95fae39c4bdd80ac", |
| 43 | + "after": "40bebe5d89b34840a573f51834a54006" |
| 44 | + } |
| 45 | + }, |
| 46 | + { |
| 47 | + "name": "Development", |
| 48 | + "ref": "2f85286bafdf439f95fae39c4bdd80ac", |
| 49 | + "version": 0, |
| 50 | + "expression": "((any(http.request.cookies[\"development\"][*] eq \"true\"))) and (cf.zone.name eq \"jfowler.theburritobot.com\")", |
| 51 | + "locked_on_deployment": false, |
| 52 | + "position": { |
| 53 | + "after": "53278ccfd7564fa69172f19b43c8b8a9" |
| 54 | + } |
| 55 | + } |
| 56 | + ] |
| 57 | + }, |
| 58 | + "success": true, |
| 59 | + "errors": [], |
| 60 | + "messages": [] |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +In this particular example, we have three environments: production, staging and development. You can find the environment ID in the `ref` field. |
| 65 | + |
| 66 | +## Step 2: Purge cache per environment |
| 67 | + |
| 68 | +To purge the production environmet, use the general cache purge endpoint: |
| 69 | + |
| 70 | +```bash |
| 71 | +https://api.cloudflare.com/client/v4/zones/<zone-tag>/purge_cache/ |
| 72 | +``` |
| 73 | + |
| 74 | +To purge the staging environment, use the following request: |
| 75 | + |
| 76 | +```bash |
| 77 | +https://api.cloudflare.com/client/v4/zones/<zone-tag>/environments/53278ccfd7564fa69172f19b43c8b8a9/purge_cache/ |
| 78 | +``` |
| 79 | + |
| 80 | +To purge the development environment, use the following request: |
| 81 | + |
| 82 | +```bash |
| 83 | +https://api.cloudflare.com/client/v4/zones/<zone-tag>/environments/2f85286bafdf439f95fae39c4bdd80ac/purge_cache/ |
| 84 | +``` |
0 commit comments