Skip to content
Start here

Invalidate Cached Content by Environment

client.cache.invalidateEnvironment(stringenvironmentID, CacheInvalidateEnvironmentParamsparams, RequestOptionsoptions?): CacheInvalidateEnvironmentResponse { id } | null
POST/zones/{zone_id}/environments/{environment_id}/invalidate_cache

Marks cached content as stale for one environment of the zone. Content cached for the zone’s other environments, including production, is not affected. Otherwise this works like POST /zones/{zone_id}/invalidate_cache: the next request for invalidated content makes Cloudflare revalidate it with your origin, and the request body takes the same fields.

Environments are part of Version Management. To delete the content instead, use POST /zones/{zone_id}/environments/{environment_id}/purge_cache.

Invalidating by URL (files) does not work for environments that select requests by IP address, country, ASN, or threat score, and fails with error 1136. Use tags, hosts, prefixes, or purge_everything for those environments.

Availability and limits

Rate limits and the number of items you can send in one request depend on your plan. See Purge cache: availability and limits.

Security
API Token

The preferred authorization scheme for interacting with the Cloudflare API. Create a token.

Example:Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY
API Email + API Key

The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

Example:X-Auth-Email: user@example.com

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

Example:X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194
Accepted Permissions (at least one required)
Zone Versioning Write
ParametersExpand Collapse
environmentID: string
maxLength32
CacheInvalidateEnvironmentParams = CachePurgeFlexPurgeByTags | CachePurgeFlexPurgeByHostnames | CachePurgeFlexPurgeByPrefixes | 3 more
CacheInvalidateEnvironmentParamsBase { zone_id, tags }
zone_id: string

Path param: The zone ID.

maxLength32
tags?: Array<string>

Body param: Cache tags. Targets all content whose Cache-Tag response header contains at least one of these tags. See Purge cache by cache-tags.

CachePurgeFlexPurgeByTags extends CacheInvalidateEnvironmentParamsBase { zone_id, tags }
CachePurgeFlexPurgeByHostnames extends CacheInvalidateEnvironmentParamsBase { zone_id, tags }
CachePurgeFlexPurgeByPrefixes extends CacheInvalidateEnvironmentParamsBase { zone_id, tags }
CachePurgeEverything extends CacheInvalidateEnvironmentParamsBase { zone_id, tags }
CachePurgeSingleFile extends CacheInvalidateEnvironmentParamsBase { zone_id, tags }
CachePurgeSingleFileWithURLAndHeaders extends CacheInvalidateEnvironmentParamsBase { zone_id, tags }
ReturnsExpand Collapse
CacheInvalidateEnvironmentResponse { id }
id: string
maxLength32

Invalidate Cached Content by Environment

import Cloudflare from 'cloudflare';

const client = new Cloudflare({
  apiToken: process.env['CLOUDFLARE_API_TOKEN'], // This is the default and can be omitted
});

const response = await client.cache.invalidateEnvironment('023e105f4ecef8ad9ca31a8372d0c353', {
  zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
  tags: ['product-1234', 'homepage'],
});

console.log(response.id);
{
  "errors": [],
  "messages": [],
  "result": {
    "id": "023e105f4ecef8ad9ca31a8372d0c353"
  },
  "success": true
}
{
  "errors": [
    {
      "code": 1092,
      "message": "Request cannot contain \"purge_everything\" and any of \"files\", \"tags\", \"hosts\" or \"prefixes\""
    }
  ],
  "messages": [],
  "result": null,
  "success": false
}
{
  "errors": [
    {
      "code": 1134,
      "message": "Unable to purge, rate limit reached. Please wait and consider throttling your request speed"
    }
  ],
  "messages": [],
  "result": null,
  "success": false
}
Returns Examples
{
  "errors": [],
  "messages": [],
  "result": {
    "id": "023e105f4ecef8ad9ca31a8372d0c353"
  },
  "success": true
}
{
  "errors": [
    {
      "code": 1092,
      "message": "Request cannot contain \"purge_everything\" and any of \"files\", \"tags\", \"hosts\" or \"prefixes\""
    }
  ],
  "messages": [],
  "result": null,
  "success": false
}
{
  "errors": [
    {
      "code": 1134,
      "message": "Unable to purge, rate limit reached. Please wait and consider throttling your request speed"
    }
  ],
  "messages": [],
  "result": null,
  "success": false
}