sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

Azure CDN - How do I delete old files when new files are uploaded using "gulp deploy-azure-storage"?

Open kbeverforden opened this issue 3 years ago • 4 comments

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?

kbeverforden avatar Apr 29 '23 11:04 kbeverforden

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

ghost avatar Apr 29 '23 11:04 ghost

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
}

kbeverforden avatar Apr 29 '23 15:04 kbeverforden

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);
    }

kbeverforden avatar Apr 29 '23 19:04 kbeverforden

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

ghost avatar May 07 '23 00:05 ghost