Joannes Vermorel
Joannes Vermorel

Reputation: 9245

How to full purge the Azure CDN through PowerShell?

The cmdlet Unpublish-AzureRmCdnEndpointContent has a -PurgeContent option, however the naive usage:

Unpublish-AzureRmCdnEndpointContent 
  -EndpointName $endpointName 
  -ProfileName $profileName 
  -ResourceGroupName $resourceGroupName 
  -PurgeContent "/*"

Only appears to purge the files at the root (aka no / in the path), and not the nested files. How can I ensure that everything gets purged from all nested levels at once?

Upvotes: 2

Views: 2894

Answers (2)

Steve
Steve

Reputation: 1065

So I tried the other answer and the command returns without error but the files weren't actually purged. I ended up writing a script that enumerated the source files on disk and purged them. You can only specify 50 at once though so I had to adapt this answer:

Powershell break a long array into a array of array with length of N in one line?

This is a little specific to what I needed since the step before is to use azcopy to put the files in storage in the first place...

$currentDirectory = get-location | select -ExpandProperty Path
$files = ls ".\" -Recurse | select -ExpandProperty FullName | %{ 
    $_.Replace($currentDirectory ,"\") } | %{ $_.Replace("\", "/") }

$counter = [pscustomobject] @{ Value = 0 }
$groups = $files | Group-Object -Property { [math]::Floor($counter.Value++ / 50) }

Import-Module azurerm

$profileName = "yourprofile"
$endpoint = "yourendpoint"
$profile = Get-AzureRmCdnProfile -ProfileName $profileName

$groups | %{
    Unpublish-AzureRmCdnEndpointContent `
        -EndpointName $endpoint `
        -ProfileName $profileName `
        -ResourceGroupName $profile.resourceGroupName `
        -PurgeContent $_.Group
}

Depending on how many files you have you may want to use Start-Job to thread it. I haven't needed to yet as it's about 5-10 seconds per call.

Hope this helps someone.

Upvotes: 0

Tom Sun
Tom Sun

Reputation: 24549

Only appears to purge the files at the root (aka no / in the path), and not the nested files. How can I ensure that everything gets purged from all nested levels at once?

According to Purge an Azure CDN endpoint, we could use "/*" to purge all folders, sub-folders and files under an endpoint with /* in the path.

Wildcard purge: Asterisk (*) may be used as a wildcard. Purge all folders, sub-folders and files under an endpoint with /* in the path.

We also could capture the network from the azure portal when we try to purge all from the endpoint.

enter image description here

We also could use the fiddler to capture the API rest during run the command.

It is the same with that we operate checking [purge all] from azure portal.

enter image description here

I test it with following code.

$endpointName = "endpoint name"
$resourceGroupName = "resource group"
$profileName = "profileName"
[string[]]$purge = @("/*")  
Unpublish-AzureRmCdnEndpointContent -EndpointName $endpointName -ProfileName $profileName -ResourceGroupName $resourceGroupName -PurgeContent $purge -Debug

Note: it takes long time to finish this task.

enter image description here

Upvotes: 1

Related Questions