Cloudflare Cache Purge: Clear CDN Cache for Fresh Content — Complete Guide
This tutorial explains how to purge (invalidate) cached content on the Cloudflare CDN. You will learn the different purge methods — single URL, tag-based, hostname prefix, and purge everything — along with when to use each one and how to automate purges via the API.
Why Cache Purge Matters
When you update content on your origin server, visitors may still receive the old cached version from Cloudflare's edge until the TTL expires. A cache purge instantly invalidates cached content across all CDN edge locations, ensuring visitors see the latest version without waiting. Without purging, you risk serving outdated pricing pages, broken JavaScript bundles, or stale API responses — which erodes user trust and can cause functional issues.
Real-world use: When DodaZIP deploys a new version of its web application, the build pipeline automatically purges all cached JavaScript and CSS bundles by tag. Within seconds, every user worldwide receives the updated files without manual intervention.
Purge Methods Overview
flowchart TD
A[Need to purge cache?] --> B{Scope of change}
B -->|Single file changed| C[Purge by URL]
B -->|Multiple related files| D[Purge by tag]
B -->|Entire section changes| E[Purge by hostname]
B -->|Full deployment| F[Purge everything]
C --> G[Fastest, lowest impact]
D --> H[Precise, batch invalidates]
E --> I[Broad scope]
F --> J[Last resort, high impact]
style A fill:#f90,color:#fff
Purge by Single URL
Use this when you update a single file — for example, updating the homepage HTML or replacing one image.
# Purge a single URL via Cloudflare API
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/index.html"]
}'
# Expected output:
# {"success":true,"result":{"id":"purge_id"}}
# Verify purge (should be MISS after purge)
curl -sI https://example.com/index.html | grep -i "cf-cache-status"
# Expected output:
# cf-cache-status: MISS
Purge by Tag (Cache Tags)
Cache tags are the most precise method for batch invalidation. You add a Cache-Tag response header from your origin, then purge all files with a specific tag.
# Origin sends Cache-Tag header
HTTP/2 200
Cache-Tag: product-page, v2-assets, footer
Content-Type: text/html
# Then purge all files tagged "v2-assets"
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 '{
"tags": ["v2-assets"]
}'
# Expected output:
# {"success":true,"result":{"id":"purge_id"}}
Purge by Hostname
Use this when every file on a specific subdomain or domain needs to be refreshed.
# Purge all cached content for a hostname
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 '{
"hosts": ["cdn.example.com"]
}'
# Expected output:
# {"success":true}
Purge Everything
This clears the entire cache for your domain. Use with caution — it sends all traffic to your origin until caches warm up again.
# Purge everything
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}'
# Expected output:
# {"success":true}
# Dashboard equivalent: Navigate to Caching > Configuration > Purge Cache
# Click "Purge Everything" and confirm.
# Warning: This may cause increased origin server load for several minutes.
Automated Purge in CI/CD
Integrate cache purging into your deployment pipeline for instant content updates.
# Example GitHub Actions step to purge after deploy
- name: Purge Cloudflare cache
run: |
curl -X POST "https://api.cloudflare.com/client/v4/zones/${{ secrets.CF_ZONE_ID }}/purge_cache" \
-H "Authorization: Bearer ${{ secrets.CF_API_TOKEN }}" \
-H "Content-Type: application/json" \
--data '{"tags": ["deploy-${{ github.sha }}"]}'
FAQ
Practice Questions
- What is the advantage of tag-based purging over single URL purging for a deployment with 50 changed files?
- Why should you avoid using Purge Everything in production during peak traffic hours?
- What response header must your origin server send to enable tag-based cache purging?
Summary
Cloudflare offers four cache purge methods: single URL (precise), tag-based (batch with context), hostname (broad scope), and purge everything (full invalidation). Tag-based purging is the recommended approach for automated deployments because it allows selective invalidation of related resources. Integrate cache purging into your CI/CD pipeline to ensure visitors always see the latest content after deployment.
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro — security-first tools for the modern web.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro