Skip to content

Instantly share code, notes, and snippets.

@jahands
Last active October 31, 2015 18:19
Show Gist options
  • Save jahands/599e9e31a3dfdb860893 to your computer and use it in GitHub Desktop.
Save jahands/599e9e31a3dfdb860893 to your computer and use it in GitHub Desktop.
Clear cache of CloudFlare resources when they are updated.
# Usage: purge_cloudflare.sh <public folder where hugo is hosted from> <base url of website>
# Example:
# purge_cloudflare.sh /var/www/ "https://example.com/"
key="<api_key>"
id="<zone_id>"
email="<email>"
inDir=$1 # eg. example.com
baseUrl=$2 # eg. example.com
# Finds all files less than a minute old.
dirs=$(find $inDir -type f \( ! -iname ".gitignore" \) -cmin 1)
if [[ ${#dirs} -gt 0 ]]; then
links="\""
for f in $dirs;
do
link=${f/$inDir/""}
links+=$baseUrl
links+=$link
links+="\",\""
done
links=${links:0:${#links}-2}
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$id/purge_cache" \
-H "X-Auth-Email: $email" \
-H "X-Auth-Key: $key" \
-H "Content-Type: application/json" \
--data "{\"files\":[$links]}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment