Curl Shell Script To Purge Cache From Stackpath CDN

in Categories Networking last updated July 30, 2020

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

Curl Shell Script To Purge Cache From Stackpath CDN

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.


Share this on:

1 comment

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

Leave a Comment