Curl Shell Script To Purge Cache From Stackpath CDN

#!/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

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source