Purge the Cloudflare cache from the terminal

After a deploy, make Cloudflare forget the old pages, without clicking through the dashboard.

SNP 108 #3B6 Filed cloudflare curl cmd

Create an API token with the "Cache Purge" permission for the zone, then:

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

The zone ID is on the zone's overview page in the dashboard. To purge just a few URLs instead of everything:

shell
--data '{"files":["https://example.com/","https://example.com/garden"]}'

I run this from the deploy script, right after the build finishes, so a deploy never leaves stale pages at the edge.