PowerShell Snippet: Clearing Cloudflare Cache with Cloudflare's API
Tags: powershell, cloudflare
Published: 2020-09-02 23:42:07

---
title: PowerShell Snippet: Clearing Cloudflare Cache with Cloudflare's API
published: true
tags: powershell, cloudflare
canonical_url: https://www.swimburger.net/blog/powershell/powershell-snippet-clearing-cloudflare-cache-with-cloudflares-api
cover_image: https://www.swimburger.net/media/1117/social-optimized.png?anchor=center&mode=crop&width=1000&height=420&rnd=131951809550000000
---

Cloudflare has many services to help speed up your web properties, scale globally, secure your site, and much more. One way Cloudflare improves the speed of your website is by caching your website closer to visitors. By default HTML isn't being cached, but using [Cloudflare PageRules](https://support.cloudflare.com/hc/en-us/articles/218411427-Understanding-and-Configuring-Cloudflare-Page-Rules-Page-Rules-Tutorial-) you can also get HTML cached. Whenever you cache something, there comes a time you'll need to refresh, purge, or clear that cache. Cloudflare provides a GUI to do this, but every action you can perform using the GUI, you can also do with Cloudflare's API. You could use the API to auto purge the cache whenever you update content in your CMS of choice, or purge the cache as part of your Continuous Delivery pipeline. Using PowerShell we'll interact with Cloudflare's API and purge their cache.

## Cloudflare API keys

Before being able to interact with the Cloudflare API, you'll need to acquire the necessary keys. For the scripts you'll need to acquire the Zone ID, and the Global API Key.  
Navigate to the overview page of your site in Cloudflare and scroll down to find the Zone ID at the bottom right.

![Cloudflare Zone ID Setting](https://www.swimburger.net/media/1113/cloudflare-zone-id.png)

After copying your Zone ID, click on 'Get your API key' which will take you to your profile. At the bottom of your profile, you'll find the following section:

![Cloudflare API keys section](https://www.swimburger.net/media/1114/cloudflare-api-keys-section.png)

Click on 'View' for the Global API key and follow the instructions to obtain the key. Keep in mind with this API key, you can perform all Cloudflare actions, for all your Cloudflare sites. [Cloudflare is making API keys to be more scoped to specific permissions and sites](https://community.cloudflare.com/t/restricted-api-keys/13647/42), but for now keep this **risk** in mind!  
With the API key and Zone ID in your possession, you can move on to interacting with the API.

## Purging Cloudflare cache using API

**Using the script below you can clear all the Cloudflare cache for your website:**

```powershell
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
```

The PowerShell script above does the following:

1. Construct the API URL to the cache\_purge action, embedding your Zone ID within
2. Setting up the required authentication headers which specifies your admin email and the API key
3. Setting up the HTTP body to a JSON string specifying to purge everything
4. Putting it all together and send an HTTP DELETE request

For more details, read the [Cloudflare API documentation for cache\purge](https://api.cloudflare.com/#zone-purge-all-files).

**Using the following script you clear cache for specific URLs:**

```powershell
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
```

This function pretty much does the same as the previous snippet, except that it will construct a different JSON payload that includes all the URLs passed on to it.

For more details, read the [Cloudflare API documentation for cache\_purge by URLs](https://api.cloudflare.com/#zone-purge-files-by-url).