Linux/Unix bash shell script to purge Cloudflare url/images from the command line

in Categories Automation & Management, Networking, Web Server last updated April 28, 2017

Here is a simple and easy to use bash shell script to purge/delete Cloudflare url/images cache from the command line without using web interface.

#!/bin/bash
# Purpose: Purge a single url using cloudflare API from the bash shell 
# Author: Vivek Gite
# License: GNU GPL v2.x+
#----------------------------------------------------------------------
## Login to Cloudflare to get API keys ##
zone_id="YOUR-Cloudflare-ID-HERE"
api_key="YOUR-Cloudflare-API-KEY-HERE"
login_id="YOUR-Cloudflare-login-name"
 
## Get url to purge ##
urls="$1"
[ "$urls" == "" ] && { echo "Usage: $0 url"; exit 1; }
 
## Let user know ##
echo "Purging $urls..."
 
## Purge it using curl command ##
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/${zone_id}/purge_cache" \
     -H "X-Auth-Email: ${login_id}" \
     -H "X-Auth-Key: ${api_key}" \
     -H "Content-Type: application/json" \
     --data "{\"files\":[\"${urls}\"]}"
 
echo ""

How do I use this shell script?

Run it as follows to purge a url named https://www.cyberciti.biz/howto/neofetch-awesome-system-info-bash-script-for-linux-unix-macos/
./script https://www.cyberciti.biz/howto/neofetch-awesome-system-info-bash-script-for-linux-unix-macos/
Sample outputs:

Purging https://www.cyberciti.biz/howto/neofetch-awesome-system-info-bash-script-for-linux-unix-macos/...
{"result":{"id":"xxxxxxxxxxxxxxxxxxxxxxx"},"success":true,"errors":[],"messages":[]}

Share this on: