Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenichi-shibata/2e978c56c9dd6b2806ba5df6e59759ad to your computer and use it in GitHub Desktop.
Save kenichi-shibata/2e978c56c9dd6b2806ba5df6e59759ad to your computer and use it in GitHub Desktop.
Executes cURL to purge the cache on Cloudflare for a specified set or all files (requires config)
#!/bin/sh
# Author: Pat Migliaccio <pat@patmigliaccio.com>
# License: MIT
EMAIL="<Acct_Email>"
ZONE="<Zone_ID>"
API_KEY="<API_Key>"
DATA="{ \"files\": [\"https://example.com/css/style.css\"] }"
# Purges all files with `-a` or `--all` flags
if [[ "$1" == "--all" || "$1" == "-a" ]]; then
echo "Purging everything..."
config[data]="{ \"purge_everything\": true }"
elif [ ! -z "$1" ]; then
echo "Command not found: $1"
exit 1
fi
# Calls Cloudflare API to Purge Files
# Reference: https://api.cloudflare.com/#zone-purge-files-by-url
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE/purge_cache" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $API_KEY" \
-H "Content-Type: application/json" \
--data "$DATA"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment