Skip to content

Instantly share code, notes, and snippets.

Last active September 12, 2021 12:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Simple PowerShell function to purge Cloudflare Cache
Quick PowerShell function to purge your CloudFlare Cache for a specific zone
This is a simple function to purge CloudFlare cache for a specific zone, can be integrated with an
Azure DevOps (or equivalent) pipeline.
Obtain your API token here:
This is typically the e-mail you login to the CloudFlare Dashboard with.
You'll need your ZoneID which you can obtain from the Cloudflare dashboard or Get-CFZones
C:\Temp> Invoke-CFPurgeCache -Token your-api-key -E-Mail your@email.address -Zone your-cf-zoneid
Author: Sterling Hammer @CDLHamma -
function Invoke-CFPurgeCache {
param (
[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $true)]
$APIURI = ''
#Build required header for authentication
$Headers = @{
'X-Auth-Key' = $Token
'X-Auth-Email' = $EMail
#Build required payload - can be later expanded to purge specific files.
$Payload = @{
purge_everything = $true
#Make the payload JSON friendly
$PayloadJSON = $Payload |ConvertTo-Json
try {
Write-Verbose -Message "$(Get-Date) :: Attempting to flush CloudFlare Cache for $($Zone)" -Verbose
$requestResult = Invoke-RestMethod -Method Post -ContentType application/json -Headers $Headers -Uri $APIURI/zones/$Zone/purge_cache -Body $PayloadJSON -ErrorAction Stop
if ($requestResult.success -eq $true) {
Write-Verbose -Message "$(Get-Date) :: Successfully cleared cache for zone $($" -Verbose
else {
Write-Verbose -Message "$(Get-Date) :: Failed to purge cache for $($" -Verbose
Write-Verbose -Message "$(Get-Date) :: Errors encountered $($requestResult.errors)"
catch {
Write-Error $_
$APIToken = "your-api-key"
$AccountEmail = "your@e-mail.address"
$MyZone = "your-cloudflare-zoneid"
Invoke-CFPurgeCache -Token $APIToken -EMail $AccountEmail -Zone $MyZone
Copy link

is it possible to clear cache on specific directory or path? can you add this features?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment