Using the Cloudflare API to clear the cache and Enable Development Mode

If you use Cloudflare for caching, clearing the cache is an important part of the deployment. You may also wish to enable Development Mode (this temporarily disables the cache).

Below, I placed instructions for doing both, adapted from a very helpful example1, with Development Mode added2.

To run this, you’ll need to fill in the account email, domain, and the API key, which you can obtain from Cloudflare’s settings.

email=gary.sieling@gmail.com
tkn=
domain=findlectures.com

zone=$(curl -s -X GET "https://api.CloudFlare.com/client/v4/zones?name=${domain}&status=active&page=1&per_page=20&order=status&direction=desc&match=all" \
-H "X-Auth-Email: ${email}" \
-H "X-Auth-Key: ${tkn}" \
-H "Content-Type: application/json")

zone=${zone:18:32}

curl -X DELETE "https://api.CloudFlare.com/client/v4/zones/${zone}/purge_cache" \
-H "X-Auth-Email: ${email}" \
-H "X-Auth-Key: ${tkn}" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'

curl -X PATCH "https://api.cloudflare.com/client/v4/zones/${zone}/settings/development_mode" \
     -H "X-Auth-Email: ${email}" \
     -H "X-Auth-Key: ${tkn}" \
     -H "Content-Type: application/json" \
     --data '{"value":"on"}'

unset zone email tkn domain

If this works, you’ll see results like this:

{"result":{"id":"9ae4406845fb8eb20edbe263d4830ec6"},
"success":true,"errors":[],"messages":[]}
{"result":{"id":"development_mode","value":"on",
"modified_on":"2016-10-18T12:06:34.161265Z","time_remaining":10800,"editable":true},
"success":true,"errors":[],"messages":[]}
  1. https://progressivethink.in/on/administration/and/clear-cloudflare-cache-on-commit/ []
  2. https://api.cloudflare.com/#zone-settings-change-development-mode-setting []