Azure CDN - How do I delete old files when new files are uploaded using "gulp deploy-azure-storage"?
What type of issue is this?
Question
What SharePoint development model, framework, SDK or API is this about?
💥 SharePoint Framework
Target SharePoint environment
SharePoint Online
What browser(s) / client(s) have you tested
- [ ] 💥 Internet Explorer
- [ ] 💥 Microsoft Edge
- [ ] 💥 Google Chrome
- [ ] 💥 FireFox
- [ ] 💥 Safari
- [ ] mobile (iOS/iPadOS)
- [ ] mobile (Android)
- [ ] not applicable
- [ ] other (enter in the "Additional environment details" area below)
Additional environment details
No response
Issue description
I successfully deployed the files for my SPFX solution using Azure CDN but, every time I do it, the old files are still in the Blob container. Is there a command to delete everything before running gulp deploy-azure-storage? Every time I deploy it adds 70 new files and the previous 70 are no longer needed.
Any way to do it with gulp? PowerShell? PnP PowerShell? Azure PowerShell?
Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.
This code does the trick but I'll leave this open in case anyone knows of a better way. I'd prefer something that doesn't have to use Azure PowerShell because I can't get the Connect command to work in VS Code.
Connect-AzAccount
$ctx = New-AzStorageContext -StorageAccountName "name_of_storage_account" -StorageAccountKey "storage_account_key"
$container = "name_of_container"
Get-AzStorageBlob -Container $container -Context $ctx | ForEach-Object {
$_ | Remove-AzStorageBlob -ICloudBlob $_.ICloudBlob -Context $ctx
}
I figured out how to do it inside the gulp file. Install the package @azure/storage-blob and use the following code
const { ContainerClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
const sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);
const containerClient = new ContainerClient(
`https://${accountName}.blob.core.windows.net/${containerName}`,
sharedKeyCredential
);
for await (const blob of containerClient.listBlobsFlat())
{
await containerClient.deleteBlob(blob.name);
}
Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues