Skip to content
Start here

Purge Cached Content by Environment

client.Cache.PurgeEnvironment(ctx, environmentID, params) (*CachePurgeEnvironmentResponse, error)
POST/zones/{zone_id}/environments/{environment_id}/purge_cache

Deletes cached content 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}/purge_cache: the next request for purged content is a cache MISS, and the request body takes the same fields.

Environments are part of Version Management. To keep content cached and have Cloudflare revalidate it instead, use POST /zones/{zone_id}/environments/{environment_id}/invalidate_cache.

Purging 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)
Cache Purge
ParametersExpand Collapse
environmentID string
maxLength32
params CachePurgeEnvironmentParams
ZoneID param.Field[string]

Path param: The zone ID.

maxLength32
Tags param.Field[[]string]Optional

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.

ReturnsExpand Collapse
type CachePurgeEnvironmentResponse struct{…}
ID string
maxLength32

Purge Cached Content by Environment

package main

import (
  "context"
  "fmt"

  "github.com/cloudflare/cloudflare-go"
  "github.com/cloudflare/cloudflare-go/cache"
  "github.com/cloudflare/cloudflare-go/option"
)

func main() {
  client := cloudflare.NewClient(
    option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
  )
  response, err := client.Cache.PurgeEnvironment(
    context.TODO(),
    "023e105f4ecef8ad9ca31a8372d0c353",
    cache.CachePurgeEnvironmentParams{
      ZoneID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
      Body: cache.CachePurgeEnvironmentParamsBodyCachePurgeFlexPurgeByTags{

      },
    },
  )
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", 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
}