Curl Shell Script To Purge Cache From Stackpath CDN

A content delivery network or content distribution network (CDN) is a system of servers containing copies of data, placed at various points in a network so as to maximize bandwidth for access to the data from clients throughout the network. Please note that the following script will only work with Stackpath CDN. You need to get API access keys using stackpath portal.

Curl Shell Script To Purge Cache From Stackpath CDN

Download the following script.

Edit using a text editor such as vim/nano and make sure you set up API credentials as follows:
stack_id=''
client_id=''
client_pwd=''

Make sure jq is installed. Install the same if not installed on your desktop/workstation. For example:
$ sudo apt install jq

Shells script

#!/usr/bin/env bash
# Usage: 
# A simple shell script to purge Stackpath cdn cache using API and curl 
# command line. Tested on Ubuntu/Debian/RHEL/Fedora Linux and FreeBSD/macOS Unix
# Syntax:
# /path/to/stackpath.purge.sh https://your-domain/url
# ----------------------------------------------------------------------------
# Written by Vivek Gite <http://www.cyberciti.biz/>
# (c) 2020 nixCraft under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated: 22/May/2012
# ----------------------------------------------------------------------------
set -e
 
################
# Set me first #
################
stack_id="YOUR-STACK-ID-HERE"
 
# APK keys 
client_id='YOUR-API-CLIENT-ID-HERE'
client_pwd='YOUR-API-AUTH-PASSWORD-HERE'
 
######################
## No editing below ##
######################
# All ulrs 
urls="$@"
 
# Make sure we get at least url else die with usage 
[ "$urls" == "" ] && { echo "Usage: $0 url1 url2 ..."; exit 1; }
 
# Get auth stuff; aka bearer
b="$(curl -s --request POST \
  --url https://gateway.stackpath.com/identity/v1/oauth2/token \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data "{
    \"client_id\": \"${client_id}\",
    \"client_secret\": \"${client_pwd}\",
    \"grant_type\": \"client_credentials\"
}" | jq '.access_token')"
 
# Clean up bearer for the curl
bearer="${b//\"/}"
 
# Let us start purging urls one-by-one
for u in $urls
do
	echo -n "Purgin url $u:"
	purge_id=$(curl -s --request POST \
	  --url https://gateway.stackpath.com/cdn/v1/stacks/$stack_id/purge \
	  --header 'accept: application/json' \
	  --header "authorization: Bearer $bearer" \
	  --header 'content-type: application/json' \
	  --data "{\"items\":[
  			{
				\"url\":\"${u}\",
				\"purgeAllDynamic\":true
			}
		  ]
	  }" | jq '.id')
 
# Get url purge status
	purge_id="${purge_id//\"/}"
  	curl -s -i --request GET \
	  --url https://gateway.stackpath.com/cdn/v1/stacks/$stack_id/purge/$purge_id \
	  --header 'accept: application/json' \
	  --header "authorization: Bearer $bearer" | egrep -o 'HTTP/.*|{.*}'
    echo ""
 
done

Usage

Run it as follows:
$ ./stackpath.purge.sh
$ ./stackpath.purge.sh https://www.cyberciti.biz

Conclusion

The script will purge cached content for CDN site on a stack. Content is re-cached on the CDN the next time it is requested. Use the returned purge ID to see the status of a purge request. See API docs here for more info.

Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
Category List 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
3 comments… add one
  • Lal K Sep 4, 2020 @ 19:20

    Very useful. thanks!

  • raj Aug 3, 2020 @ 15:53

    Very nice script. We started using Stackpath recently and this was useful. Can we do the same with PHP based application?

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.