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

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":[]}
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.