If you're searching for this, then you already know what is Cloudflare and what is does.
So, straight to the point, how you can clear all the cache or cache for a specific item from Cloudflare CDN with UNIX command line tools to automate your task and save time.
Contents
What will you need?
You'll need the Cloudflare API key for your account, the Zone ID associated with the particular domain and the email address of the account.
Thus, if you've not the API key yet, login to your Cloudflare account generate an API key.
The Zone ID is different for different domains.
How to delete Cloudflare cache with command line
To clear Cloudflare cache with command line, use the cURL command to send the proper API requests to Cloudflare. Have a look at the sample command below.
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/xxxxxxxxxxxxxxxxxxxx/purge_cache" \
-H "X-Auth-Email: your.name@mail.com" \
-H "X-Auth-Key: yyyyyyyyyyyyyyyyyyyyyyyyyyyyy" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'
The response is returned in JSON format. If the success field is true, then the meaning is obvious.
This command deletes all the cache associated with the particular domain. It's actually a single line command, I've broken it down for easier understanding.
Here xxxxxxxxxxxxxxxxxxxx is the Zone ID and yyyyyyyyyyyyyyyyyyyyy is the Cloudflare API key. You've to replace the zone ID and API key with the proper one for your domain.
Another example to delete particular file
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/xxxxxxxxxxxxxxxxxxxx/purge_cache" \
-H "X-Auth-Email: your.name@mail.com" \
-H "X-Auth-Key: yyyyyyyyyyyyyyyyyyyyyyyyyyyyy" \
-H "Content-Type: application/json" \
--data '{"files":["'https://www.example.com/style.css'"]}'
Again if everything is done correctly, it should return the success status true, like below.
{
"result": {
"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"success": true,
"errors": [],
"messages": []
}
FYI- you can put many files together inside the JSON array to purge cache of multiple files/URLs with a single API request.
UPDATE - Use API tokens for Authorization
CloudFlare introduced a token based access control to their API, which don't require any username or password. This method is far better for scripting and automation.
I'm not going to elaborate how to create those tokens, you can read the details here. Just make sure to give it sufficient permission to purge cache.
Also replace the -H "X-Auth-Email: and -H "X-Auth-Key: lines with a single authorization header.
-H "Authorization: Bearer *************************"
Replace the ******* with the actual API token.
Shell script to clear CloudFlare cache
You might want to quickly delete the cache without opening the web browser and logging in to the Cloudflare account, so here's a script to purge individual URL cache.
!/bin/bash my_url=$@ curl -s -X POST "https://api.cloudflare.com/client/v4/zones/xxxxxxxxxxxxxxxxxxx/purge_cache" \ -H "X-Auth-Email: my.emailk0@gmail.com" \ -H "X-Auth-Key: yyyyyyyyyyyyyyyyyyyyyyyyyyy" \ -H "Content-Type: application/json" \ --data '{"files":["'$my_url'"]}'
Save this with any name you want, I'm using cf_delete_cache as an example. Then make this script executable with chmod +x cf_delete_cache
.
To use this script, simply run it like below.
./cf_delete_cache https://my-site.com/some-random-url/
You can also use this script with cron jobs to further automate the task, like automated cache purging of anything specific, etc. etc.
Hope you found this useful and informative, thank you.
Romain says
Hello,
Thank you for this post.
I have some dificulty to apply your comand...
I found this
curl -X GET "https://api.cloudflare.com/client/v4/zones?name=example.com&status=active&account.id=01a7362d577a6c3019a474fd6f485823&account.name=Demo Account&page=1&per_page=20&order=status&direction=desc&match=all" \
-H "X-Auth-Email: user@example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json"
Here : https://api.cloudflare.com/#zone-list-zones
What is the hell !? lol
Arnab Satapathi says
Listing zones is unrelated to cache delete.
Also you need to send POST requests through cURL, not GET.