How do I force Azure CDN content to be purged or invalidated?
Asked Answered
G

5

15

I have content that rarely changes that I want to serve over the Azure CDN for performance reasons. When the content does change, though, it's important that the updated data is immediately available. Ideally, I'd be able to set a long TTL but then proactively tell the CDN to expire content when I update it. How can I accomplish this? There is no cache invalidation or purge API right now, and I would rather not set a short TTL.

Gorgon answered 6/1, 2012 at 7:16 Comment(0)
M
11

In 2015 December the Azure team added the ability to refresh or purge the CDN via REST API (https://msdn.microsoft.com/en-us/library/mt634451.aspx). In the beginning, this function is only available for endpoints created with the new Azure Portal (http://portal.azure.com), however, CDNs created with the old management surface will be migrated in the beginning of 2016 (https://feedback.azure.com/forums/169397-cdn/suggestions/556307-ability-to-force-the-cdn-to-refresh-any-cached-con).

Maltreat answered 18/12, 2015 at 9:42 Comment(2)
Is this going to purge whole endpoints at a time, or can you purge individual blobs?Lyndialyndon
@jz87: you can purge specific content paths (The path to the content to be purged or loaded) with the new API. See msdn.microsoft.com/en-us/library/mt634451.aspxNickolasnickolaus
A
8

There is NO API to invalidate an Azure CDN.

Workaround:

  1. Enable "Query String Status" on your CDN in Azure portal. Then you can append a new query string name and random value eg. /images/background.png?v=1234

  2. Upload and rename the new file with a timestamp or random value. for example: /images/background.20140917225200.png

  3. Set a shorter cache header and wait for it to expire. Here's an article from the Azure team http://msdn.microsoft.com/en-us/library/azure/gg680306.aspx

Arlettaarlette answered 7/1, 2012 at 21:18 Comment(2)
Thanks, that is basically ther route I took, we have a DataVersion that we increment whenever it changes, and the clients makes an Out of Bend REST call to find the latest DataVersion, then request a file containing that in the filename from the CDN.Gorgon
We've had the same problem with Azure serving files after they had changed but before TTL expiry. We've switched to Amazon S3, it allows invalidations (note it costs money). Plus in our case it better serves asia, australia and south america.Fetor
D
2

You can use the az cli to purge the cdn endpoint

az cdn endpoint purge is the root command

an example

az cdn endpoint purge -g group -n endpoint \
--profile-name profile-name \
--content-paths '/scripts/app.js' '/styles/*'

You can find more information on the cli command here

Digamy answered 1/8, 2019 at 1:21 Comment(0)
S
1

You can also purge CDN content via PowerShell:

$AzureCdnResourceGroupName = "--RESOURCE GROUP--"
$AzureCdnEndpoint = "--ENDPOINT NAME--"
$AzureCdnProfileName = "--CDN PROFILE NAME--"

#In case there's multiple subscriptions on the account
Set-AzureRmContext -SubscriptionId $AzureCdnSubscriptionId
$AzureCdnSubscriptionId = "--SUBSCRIPTION GUID--"

Write-Host "Purging CDN $AzureCdnProfileName/$AzureCdnEndpoint"

Invoke-AzureRmResourceAction `
    -ResourceGroupName $AzureCdnResourceGroupName `
    -ResourceType 'Microsoft.Cdn/profiles/endpoints' `
    -ResourceName $AzureCdnProfileName/$AzureCdnEndpoint `
    -ApiVersion '2015-06-01' `
    -Action 'Purge' `
    -Parameters @{ ContentPaths = @('/static/js/*','/static/css/*') } `
    -Force

Write-Host 'Purging completed'
Siderostat answered 23/8, 2018 at 14:26 Comment(0)
P
-1

You cannot force CDN purge.

Best practice is to append version/date info to your file name, and design your app to dynamically get the current file name.

For product photos as an example, append the version to the blob name, store the blob name in a table, and then serve a link to the name from the table, instead of hard-coding the filename.

This way you can set max-expiry on the cache headers, and Azure will just clean up stale content when it needs to.

Pachton answered 3/2, 2012 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.