Will Browning.

7th Aug 18 (Updated: 17th Jul 19) #deployment 1 min read

Clearing Your Cloudflare Cache After New Deployments

I had lost count of the number of times I've previously deployed new code to production, cleared browser cache and still been unable to see the changes. The reason being that Cloudflare was caching these files and still serving the old files. Luckily there is a really easy way to clear the cache and purge everything using the API.

Getting your Cloudflare API key

First we need to find our API key. So visit the "My Profile" section when logged into Cloudflare - https://dash.cloudflare.com/profile at the bottom of the page you'll see your keys.

Cloudflare API keys

We'll be using the Global API Key to clear the cache.

Finding your sites Zone ID

The Zone ID for your website can be found in the "Overview" section for that site. It will look like this:

Cloudflare Zone ID

Sending the API request

The request we'll be sending to clear the cache looks like this:

curl -X POST "https://api.cloudflare.com/client/v4/zones/YOUR-ZONE-ID/purge_cache" \
     -H "X-Auth-Email: YOUR-CLOUDFLARE-EMAIL" \
     -H "X-Auth-Key: YOUR-GLOBAL-API-KEY" \
     -H "Content-Type: application/json" \
     --data '{"purge_everything":true}'

Where YOUR-CLOUDFLARE-EMAIL is the email you use to login to Cloudflare. YOUR-GLOBAL-API-KEY is the key we found above and where YOUR-ZONE-ID is a unique identifier for your Cloudflare website.

You can add this code to the end of your deployment script to make sure the cache is purged after each deployment.

In the above example we're telling Cloudflare to purge everything but you can also choose which items to purge that have matching Cache-Tag headers or which hosts to purge although this does appear to be only available for Enterprise accounts.

--data '{

More documentation can be found here - https://api.cloudflare.com/#zone-purge-files-by-cache-tags-or-host

If everything went to plan you should get a response from Cloudflare like so:

  "success": true,
  "errors": [],
  "messages": [],
  "result": {
    "id": "9a7806061c88ada191ed06f989cc3dac"
Like what you've seen here?

Fire over your email and I'll keep you updated once a month about any new posts. No Spam.