Not long ago, one of my colleagues asked me how he could automate the purge of an Azure CDN endpoint. I browsed around a bit with him and the only documentation we've found talked about calling the Azure CDN REST API, meaning acquiring a bearer token and all. I said to myself, there must be something simpler out there but what? For those who know me, I prefer to use PowerShell or Azure-CLI whenever possible and in this article, I'll show you how to achieve the very same result using native Azure PowerShell v1.x commands.

UPDATE 2016-05-11: This article has been updated following discussions with Kuangwei Zhang & Cam Soper from Microsoft, thank you guys.

Why purge?

Here is an extract from the Azure documentation that explains well why you would want to purge your CDN endpoint.

Sometimes you may wish to purge cached content from all edge nodes and force them all to retrieve new updated assets. This might be due to updates to your web application, or to quickly update assets that contain incorrect information.

Let's dig into the AzureRM.Cdn module

If we look at what is available in v1.0.1 of the AzureRM.Cdn module, you'll see that there's no native commands with the Purge keyword in there. This is because purge is not an approved verb in PowerShell. The team decided to go with the Publish/Unpublish combinaison.

Get-Command -Module AzureRM.Cdn

List of available commands in v1.0.1 of AzureRM.Cdn

The Solution

If you need to determine your CDN profile and endpoint name, you can use the Azure Portal or Azure Resource Explorer. Here is how you can determine the required values in Resource Explorer:

CDN profiles and endpoints in Azure Resource Explorer

And finally how to invoke the purge command using Unpublish-AzureRmCdnEndpointContent:

Unpublish-AzureRmCdnEndpointContent `
    -ResourceGroupName $ResourceGroupName `
    -ProfileName $ProfileName `
    -EndpointName $EndpointName `
    -PurgeContent '/*'

or if you already have your endpoint object in hand

$endpoint = Get-AzureRmCdnEndpoint -ResourceGroupName $ResourceGroupName `
    -ResourceGroupName $ResourceGroupName `
    -ProfileName $ProfileName `
    -EndpointName $EndpointName

$endpoint | Unpublish-AzureRmCdnEndpointContent -PurgeContent = '/*'

Happy automation with the Azure CDN!

Reference