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":[]}

How to deal with multiple urls?

We can use bash for loop. For instance:

#!/bin/bash
# Purpose: Purge url(s) 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(s) to purge ##
urls="$*"
[ "$urls" == "" ] && { echo "Usage: $0 url"; exit 1; }
 
## Let user know ##
echo "Purging $urls..."
 
## Purge it using curl command ##
for u in $urls
do
      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\":[\"${u}\"]}"
 
echo ""
done

Then we can run it as:
./script url1 url2 ...
See the comment section below for other ideas.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics via:
Category List of Unix and Linux commands
Disk space analyzers df ncdu pydf
File Management cat tree
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities NetHogs 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
1 comment… add one
  • Nikki Smith May 4, 2021 @ 19:46

    Thanks for writing this. I’ve extended it to (optionally) accept multiple urls that are space-separated on the command line:

    #!/bin/bash
    # Purpose: Purge multiple url(s) using cloudflare API from the bash shell
    # See https://api.cloudflare.com/#zone-purge-files-by-url
    # Author: Vivek Gite and Nikki Smith
    # 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-EMAIL"
    
    ## Get url(s) to purge ##
    urls="$@"
    [ "$urls" == "" ] && { echo "Usage: $0 url [url]"; exit 1; }
    
    ## Let user know ##
    echo "Purging $urls..."
    
    ## Purge it using curl command ##
    files="${urls// /\",\"}"
    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\":[\"${files}\"]}"
    
    echo ""
    

Leave a Reply

Your email address will not be published.

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