Skip to content

Instantly share code, notes, and snippets.

@lucien144
Last active May 19, 2024 21:24
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}'
@DanCanetti
Copy link

Works like a charm. Thanks!

@thebitresearch
Copy link

Great thanks alot ! I just used POST (because that's how its written in the documentation) instead of DELETE and worked instantly.

@lucien144
Copy link
Author

They must have changed it, I'll update the gist accordingly.

@thebitresearch
Copy link

thanks again, saved me some time!

@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