Skip to content

Instantly share code, notes, and snippets.

@patmigliaccio
Last active September 13, 2023 01:47
Show Gist options
  • Save patmigliaccio/fbdbb4c0192deddc229b16ef62347a74 to your computer and use it in GitHub Desktop.
Save patmigliaccio/fbdbb4c0192deddc229b16ef62347a74 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"
@SobolevskyDmitry
Copy link

👍

@bagusnl
Copy link

bagusnl commented Aug 27, 2023

Thank you so much for the command!
I modified mine so it works under github actions and only use the Token ID instead of also the email though, here it is

          curl -X POST "https://api.cloudflare.com/client/v4/zones/${{ secrets.ZONE_ID }}/purge_cache" \
               -H "Authorization: Bearer ${{ secrets.API_TOKEN }}" \
               -H "Content-Type: application/json" \
               --data "{  \"purge_everything\": true }"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment