Instantly Delete Cloudflare Pages and 404 Them

Cloudflare Pages doesn't do this on its own very quickly. With our integration, we now supersede this default.

Here’s the scenario: you delete a file from your Cloudflare Pages project, deploy your changes, double-check the repo like a hawk… and yet the file is still there.

Why? Well, Network Caching of course.

The Problem: Cloudflare Ghost Files That Won’t Die

Cloudflare Pages aggressively caches files. That includes old files from previous deployments, even ones you explicitly removed. If those files were part of a past deploy, they might stick around in edge caches for hours, days, or even a week.

Even worse, those files can be accessed directly by URL. So if someone bookmarked a now-deleted file? Or if it was indexed by search engines? Or linked on social? Boom. It’s still serving. Still public. Still causing headaches.

It sounds too ridiculous to be true, but it is:

If a request is made for a file that does not exist, but existed in the previous deployment, and Pages happens to still have that file cached, it will serve it up. It won’t stick around forever but it could be a week or more.

Why It Matters: We Use Cloudflare

The Headless Hostman relies on a complex infrastructure ultimate serving on Cloudflare Static. So we found more recently this was becoming an issue for our sites and customers.

Cloudflare’s Fix: “Just Wait, Maybe Try Purging a Bit”

  • Wait it out. Seriously. Just wait.
  • Try a manual cache purge. URL by URL.
  • Purge everything. Not great if you have an entire site to maintain.
  • Set a redirect. Which you now have to remember to remove later.

Sure, these steps technically work but often times do not.

We’ve tried clearing cache from the Cloudflare-managed DNS and it doesn’t reliable solve this problem.

Our Fix: We Built a Mechanism to Track and Instantly 404 Deleted Pages

instantly delete cloudflare pages without waiting

We weren’t okay with this. If you remove a file from your project and push a new build, that file should be gone. Period.

So we built our own 404 system.

Now, when a file disappears from your latest deployment, Headless Hostman automatically intercepts requests to that file and returns a proper 404 instantly. This is done from the Network Edge, so it doesn’t slow down your site load.

Even if Cloudflare’s cache still holds onto the file behind the scenes, it’ll never be served again.

Unless, of course, you republish the page.

Fixing Every Push Situation on the Headless Hostman

Deleting

  1. If you click “Delete from Headless” we add it to the 404 tracker
  2. If you use the “Delete Orphans” tool we add each deletion to the 404 tracker

Pushing

This is key. If you happen to re-publish a page, we recheck it on every push:

  1. Full Pushes
  2. Static Pushes
  3. Post Mapping
  4. Bulk Pushes
  5. Single Page Push

Smarter Tracking

Since Cloudflare only hangs onto stale cache for up to a week, we assign a date stamp in the 404 log.

If it’s been more than seven days, we remove the page from our 404 log. This because Cloudflare will no longer serve it on its own by this point, and because we want to keep the log as lightweight as possible.

301 Redirects Still Work

Let’s say that instead of serving a 404 you want to 301 Redirect.

Any 301 Redirect will supersede our 404 tracker.

Another Static Problem solved by the Headless Hostman.

ready to get started?

Headless Hostman takes the best of both traditional CMS systems and other static host providers to create a site that is both easy to manage, fast, and secure.