How to Clear Cloudflare Cache using a webhook

How to Clear Cloudflare Cache using a webhook
Date
Tags
CloudFlare
Cache
Networking
Coolify

Automatically purge Cloudflare cache using webhooks and API tokens.

When deploying web apps, caching can often get in the way of seeing fresh changes instantly. If you’re using Cloudflare for CDN and DNS, you can automate cache purging as part of your deployment process.

Prerequisites

Before you begin, make sure you have:

  • A Cloudflare account
  • Your Zone ID (found in the Cloudflare dashboard under your domain settings)

Create a Cloudflare API Token

  1. Go to your Cloudflare API Tokens page.
  2. Click “Create Token”.
  3. Select “Custom Token” and grant these permissions:
    • Zone.Zone → Read
    • Zone.Cache Purge → Purge
  4. Scope the token to the specific zone (domain) you’re working with.
  5. Save the token securely — you’ll need it shortly.

Test the token

You can test your token using curl:

curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
     -H "Authorization: Bearer [__CF_TOKEN__]" \
     -H "Content-Type:application/json"

Don’t forget to replace [__CF_TOKEN__] with your actual token.

Purging the Cache

To purge the cache, you can use the following curl command. Replace [__CF_ZONE_ID__] with your Cloudflare Zone ID and [__CF_TOKEN__] with your API token:

curl -X POST "https://api.cloudflare.com/client/v4/zones/[__CF_ZONE_ID__]/purge_cache" \
     -H "Authorization: Bearer [__CF_TOKEN__]" \
     -H "Content-Type: application/json" \
     --data '{"purge_everything":true}'

Optional: Purge Specific Files

If you want to clear only certain files (e.g., /index.html, /styles.css), change the data payload:

--data '{"files":["https://yourdomain.com/index.html", "https://yourdomain.com/styles.css"]}'

Integrate with Coolify

If you are using the fantastic PaaS Coolify for your deployments, you can automate the cache purging process by adding a post-deployment script.

In your Coolify project configuration, scroll to the “Post-Deployment Script” section and add the curl command you created above.

This command will:

  • Run automatically after each successful deploy.
  • Clear all cached assets from Cloudflare for your domain.

Now every time you deploy through Coolify, Cloudflare’s cache will be purged, ensuring that your latest changes are immediately reflected.

More Articles