Runar Ovesen Hjerpbakk

Software Philosopher

Automatically purge changed pages from the Cloudflare cache when adding a new post to Jekyll

This site is built with Jekyll, hosted on GitHub Pages, has DNS through Hover and is served by the Cloudflare CDN. The site is static, that is all pages are rendered ahead of time on the server. That means that even the simplest web server in the world could host without issue. So why is Cloudflare a part of this stack?

I put it behind Cloudflare for three reasons:

  • Real simple TLS
  • Security, Cloudflare can protect you from DDoS attacks (not that this is an issue for me) and has a looong list of known bad players which are blocked from accessing your site.
  • Performance, Cloudflare has a global CDN and an excellent cache. Thus, your own server is hit far less than without this outer layer.

Since Cloudflare has a cache, how do you make sure the right parts of the cache are invalidated when you create new content? With a simple dotnet script that’s how!

I’ve said it before, dotnet script together with Visual Studio Code, is entirely amazing! Scripting using C#, the world greatest programming language, with all the available packages on nuget available to you, running on any OS!

Cloudflare Cache purger

My script purges relevant pages from the cache on Cloudflare when adding a new post to a Jekyll repository. In addition to the tag pages matching the tags of the latest post and the post itself, it purges the following hardcoded pages:

  • .
  • . /feed.xml
  • . /sitemap.xml
  • ./archive

Running locally

dotnet script main.csx -- path_to_jekyll_repo

Running from GitHub

dotnet script -- path_to_jekyll_repo

Caution, do not run scripts from the Internet that you do not fully understand.


The script takes one argument. This is the path to the Jekyll repo.

If a config.json file is added to the same directory, the script will try to actually purge the cache. If this file is missing, the script only prints what it would’ve done.

config.json should look like this:

   "baseAddress": "[baseAdress]",
   "cloudflareApiKey": "[cloudflareApiKey]",
   "cloudflareEmail": "[cloudflareEmail]",
   "cloudflareZoneId": "[cloudflareZoneId]"
  • baseAdress is the URL to your site.
  • cloudflareApiKey is your Cloudflare API key. You can see this on your profile.
  • cloudflareEmail is the email to your user on Cloudflare.
  • cloudflareZoneId is the id of this site on Cloudflare. You can see this in the overview tab for your site.

Dry run

If the script cannot find config.json, the generated paths will only be written to standard out. The default site is, but this can easily be changed to your domain within the script.

Running the script without configuration

Purge Cloudflare cache

To use this with Cloudflare, clone the repo, create config.json from config.default.json and input your configuration values.

Running the script now will call the Cloudflare API and do the needful.

Result of running the script connected to Cloudflare