Back to Blog
Lesson 7 of the Cloudflare: Cloudflare for Developers: DNS to CDN course
Cloud NativeJuly 8, 20264 min read

Managing Cache via Dashboard and API: A Developer's Guide

Learn how to purge Cloudflare cache via the dashboard and API. Master targeted file invalidation and global purges to keep your edge content fresh and accurate.

CloudflareCacheAPIDevOpsCDNPerformance

Previously in this course, we explored Global Network and Edge Caching and learned how to set up Configuring Cache Rules to dictate how long assets stay at the edge. However, even with perfect TTL settings, you will eventually reach a point where you need to force an update before the timer runs out.

That is where cache purging comes in. Whether you've pushed a hotfix to a CSS file or updated a critical API response, knowing how to invalidate your edge content is a core skill for any developer managing production traffic.

Understanding Purge Strategies

When you "purge" the cache, you are essentially telling Cloudflare's edge nodes to discard their current copy of an asset. The next time a user requests that resource, the edge node will fetch a fresh copy from your origin server.

There are three primary ways to handle this:

  1. Purge Everything: The "nuclear option." It flushes the entire cache for your zone. Use this sparingly, as it causes a sudden spike in traffic back to your origin server (a "cache stampede").
  2. Purge by URL: Targeted invalidation. You specify the exact file path (e.g., https://example.com/assets/style.css). This is the most efficient way to handle specific updates.
  3. Purge by Tag: Using Cache-Tags (Surrogate Keys), you can group related assets and purge them all at once. This is excellent for clearing all images or components associated with a specific database entry.

Purging via the Dashboard

The Cloudflare Dashboard provides a simple interface for manual intervention.

  1. Log in and select your domain.
  2. Navigate to Caching > Configuration.
  3. Click the Purge Cache button.

Here, you can choose Custom Purge to enter specific URLs, or Purge Everything if you're dealing with a site-wide configuration change. While convenient for one-off fixes, manual purging doesn't scale. For automated workflows—like integrating with your CI/CD pipeline—you need the API.

Automating Purges with the Cloudflare API

To trigger a purge programmatically, you'll need your Zone ID and an API Token with the Cache Purge: Edit permission.

Worked Example: Purging by URL using cURL

If you have updated style.css and want to ensure users see it immediately, you can send a POST request to the Cloudflare API.

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 '{
        "files": [
          "https://example.com/css/style.css",
          "https://example.com/images/logo.png"
        ]
     }'

Replace {ZONE_ID} and {API_TOKEN} with your actual values. This command returns a JSON object confirming the request was accepted.

Purging Everything via API

If you must clear everything, the payload changes slightly:

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}'

Hands-on Exercise

Your task is to verify that your cache is working and then clear it.

  1. Open your browser's Network tab and request a static asset from your site. Check the cf-cache-status header to confirm it says HIT.
  2. Use the Dashboard to "Purge Everything."
  3. Refresh the page. The cf-cache-status should now show MISS (or EXPIRED), confirming the edge had to re-fetch the file from your origin.
  4. If you have an API token ready, try the files purge method to invalidate only your CSS file.

Common Pitfalls

  • The Cache Stampede: Purging everything on a high-traffic site during peak hours can crash your origin server. Because the edge is suddenly empty, every single incoming request hits your server simultaneously.
  • Case Sensitivity: Purge URLs are case-sensitive. If you request Style.css but purge style.css, the cache will not be cleared.
  • API Rate Limits: Cloudflare limits how many times you can call the purge API per minute. If you are building a system that purges on every single file save, you might hit these limits. Always batch your purge requests into a single API call when possible.

FAQ

Q: How long does it take for a purge to propagate? A: Purges usually propagate globally within a few seconds.

Q: Does purging the cache delete my files? A: No. Purging only clears the cached copy at the Cloudflare edge. Your files remain safe on your origin server.

Q: Can I automate this for my deployments? A: Yes, as discussed in Automating Cloudflare Cache API Purging for Vercel Deployments, it is standard practice to add a build step that calls this API after your code is deployed.

Recap

We've covered how to perform targeted purges via the dashboard and how to automate these processes using the Cloudflare API. By moving away from manual "Purge Everything" calls toward targeted URL or tag-based invalidation, you maintain high performance without overwhelming your origin server.

Up next: We will dive into Image Optimization and Resizing to make your asset delivery even faster.

Similar Posts