Skip to content

Instantly share code, notes, and snippets.

@lucien144
Last active July 20, 2024 20:36
Show Gist options
  • Save lucien144/832e9493a4a599303df3200863daa450 to your computer and use it in GitHub Desktop.
Save lucien144/832e9493a4a599303df3200863daa450 to your computer and use it in GitHub Desktop.
Clear Cloudflare cache from command line.
#!/bin/bash
curl -X POST "https://api.cloudflare.com/client/v4/zones/{$ZONE_ID}/purge_cache" \
-H "X-Auth-Email: {$EMAIL}" \
-H "X-Auth-Key: {$API_KEY}" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'
@hasanelahi
Copy link

Thank you...

@Matt-Welland
Copy link

Matt-Welland commented Jun 22, 2023

Fantastic, I had to remove the X-Auth-Email and X-Auth-Key and replace with the authorization Cloudflare generated.

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

@bluegreen08
Copy link

How to use this for custom purge? URL based

@lucien144
Copy link
Author

@bluegreen08 Most likely like this:

curl -X POST "https://api.cloudflare.com/client/v4/zones/{$ZONE_ID}/purge_cache" \
     -H "X-Auth-Email: {$EMAIL}" \
     -H "X-Auth-Key: {$API_KEY}" \
     -H "Content-Type: application/json" \
     --data '{"prefixes":["www.example.com/foo","images.example.com/bar/baz"]}'

@bluegreen08
Copy link

@bluegreen08 Most likely like this:

curl -X POST "https://api.cloudflare.com/client/v4/zones/{$ZONE_ID}/purge_cache" \
     -H "X-Auth-Email: {$EMAIL}" \
     -H "X-Auth-Key: {$API_KEY}" \
     -H "Content-Type: application/json" \
     --data '{"prefixes":["www.example.com/foo","images.example.com/bar/baz"]}'

Thank you!!

@bluegreen08
Copy link

@bluegreen08 Most likely like this:

curl -X POST "https://api.cloudflare.com/client/v4/zones/{$ZONE_ID}/purge_cache" \
     -H "X-Auth-Email: {$EMAIL}" \
     -H "X-Auth-Key: {$API_KEY}" \
     -H "Content-Type: application/json" \
     --data '{"prefixes":["www.example.com/foo","images.example.com/bar/baz"]}'

I realized this is tied to mu specific email. Is there a way to have this command and use a service token or service account?

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