|
| 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 by sending a request to the following API endpoint: |
| 14 | + |
| 15 | +```bash |
| 16 | +https://api.cloudflare.com/client/v4/zones/<zone_id>/environments |
| 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": "12abcd3e45f678940a573f51834a54", |
| 28 | + "version": 0, |
| 29 | + "expression": "(cf.zone.name eq \"example.com\")", |
| 30 | + "locked_on_deployment": false, |
| 31 | + "position": { |
| 32 | + "before": "5d41402abc4b2a76b9719d911017c" |
| 33 | + } |
| 34 | + }, |
| 35 | + { |
| 36 | + "name": "Staging", |
| 37 | + "ref": "5d41402abc4b2a76b9719d911017c", |
| 38 | + "version": 0, |
| 39 | + "expression": "((cf.edge.server_ip in {1.2.3.4 5.6.7.8})) and (cf.zone.name eq \"example.com\")", |
| 40 | + "locked_on_deployment": false, |
| 41 | + "position": { |
| 42 | + "before": "49f0bad299687c62334182178bfd", |
| 43 | + "after": "12abcd3e45f678940a573f51834a54" |
| 44 | + } |
| 45 | + }, |
| 46 | + { |
| 47 | + "name": "Development", |
| 48 | + "ref": "49f0bad299687c62334182178bfd", |
| 49 | + "version": 0, |
| 50 | + "expression": "((any(http.request.cookies[\"development\"][*] eq \"true\"))) and (cf.zone.name eq \"example.com\")", |
| 51 | + "locked_on_deployment": false, |
| 52 | + "position": { |
| 53 | + "after": "5d41402abc4b2a76b9719d911017c" |
| 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 environment, use the general cache purge endpoint: |
| 69 | + |
| 70 | +```bash |
| 71 | +https://api.cloudflare.com/client/v4/zones/<zone_id>/purge_cache/ |
| 72 | +``` |
| 73 | + |
| 74 | +To purge non-production environments, you must use a new `purge_cache` endpoint and specify the environment you would like to purge. |
| 75 | + |
| 76 | +To purge the Staging environment from the example above, send a request to the following endpoint: |
| 77 | + |
| 78 | +```bash |
| 79 | +https://api.cloudflare.com/client/v4/zones/<zone_id>/environments/5d41402abc4b2a76b9719d911017c/purge_cache/ |
| 80 | +``` |
| 81 | + |
| 82 | +To purge the Development environment from the example above, send a request to the following endpoint: |
| 83 | + |
| 84 | +```bash |
| 85 | +https://api.cloudflare.com/client/v4/zones/<zone_id>/environments/49f0bad299687c62334182178bfd/purge_cache/ |
| 86 | +``` |
| 87 | + |
| 88 | +:::note |
| 89 | +When purging everything for a non-production cache environment, all files for that specific cache environment will be purged. However, when purging everything for the production environment, all files will be purged across all environments. |
| 90 | +::: |
0 commit comments