Instantly share code, notes, and snippets.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment