Skip to content

Instantly share code, notes, and snippets.

@Swimburger
Last active November 4, 2020 04:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swimburger/14db0ce72d58698facddfe0b5a83496e to your computer and use it in GitHub Desktop.
Save Swimburger/14db0ce72d58698facddfe0b5a83496e to your computer and use it in GitHub Desktop.
Purge Cloudflare Cache using Cloudflare's API in PowerShell, learn more at https://swimburger.net/blog/powershell/powershell-snippet-clearing-cloudflare-cache-with-cloudflares-api
Function PurgeAllCloudflareCache{
Param(
[parameter(Mandatory=$true)]
[string] $AdminEmail,
[parameter(Mandatory=$true)]
[string] $ApiKey,
[parameter(Mandatory=$true)]
[string] $ZoneId
);
$PurgeCacheUri = "https://api.cloudflare.com/client/v4/zones/$ZoneId/purge_cache";
$RequestHeader = @{
'X-Auth-Email' = $AdminEmail
'X-Auth-Key' = $ApiKey
};
$RequestBody = '{"purge_everything":true}';
Invoke-RestMethod `
-Uri $PurgeCacheUri `
-Method Delete `
-ContentType "application/json" `
-Headers $requestHeader `
-Body $RequestBody
}
Function PurgeCloudflareCacheByUrls{
Param(
[parameter(Mandatory=$true)]
[string[]] $UrlsToPurge,
[parameter(Mandatory=$true)]
[string] $AdminEmail,
[parameter(Mandatory=$true)]
[string] $ApiKey,
[parameter(Mandatory=$true)]
[string] $ZoneId
);
$PurgeCacheUri = "https://api.cloudflare.com/client/v4/zones/$ZoneId/purge_cache";
$RequestHeader = @{
'X-Auth-Email' = $AdminEmail
'X-Auth-Key' = $ApiKey
};
$RequestBody = @{ files = $UrlsToPurge } | ConvertTo-Json -Compress;
Invoke-RestMethod `
-Uri $PurgeCacheUri `
-Method Delete `
-ContentType "application/json" `
-Headers $requestHeader `
-Body $RequestBody
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment