@@ -123,8 +123,8 @@ filter Remove-PurgeableResources {
123123 switch ($r.AzsdkResourceType ) {
124124 ' Key Vault' {
125125 if ($r.EnablePurgeProtection ) {
126- # We will try anyway but will ignore errors.
127126 Write-Warning " Key Vault '$ ( $r.VaultName ) ' has purge protection enabled and may not be purged until $ ( $r.ScheduledPurgeDate ) "
127+ continue
128128 }
129129
130130 # Use `-AsJob` to start a lightweight, cancellable job and pass to `Wait-PurgeableResoruceJob` for consistent behavior.
@@ -134,8 +134,8 @@ filter Remove-PurgeableResources {
134134
135135 ' Managed HSM' {
136136 if ($r.EnablePurgeProtection ) {
137- # We will try anyway but will ignore errors.
138137 Write-Warning " Managed HSM '$ ( $r.Name ) ' has purge protection enabled and may not be purged until $ ( $r.ScheduledPurgeDate ) "
138+ continue
139139 }
140140
141141 # Use `GetNewClosure()` on the `-Action` ScriptBlock to make sure variables are captured.
@@ -313,14 +313,16 @@ function RemoveStorageAccount($Account) {
313313 if ($Account.Kind -eq " FileStorage" ) { return }
314314
315315 $containers = New-AzStorageContext - StorageAccountName $Account.StorageAccountName | Get-AzStorageContainer
316- $blobs = $containers | Get-AzStorageBlob
317316 $deleteNow = @ ()
318317
319318 try {
320- foreach ($blob in $blobs ) {
321- $shouldDelete = EnableBlobDeletion - Blob $blob - StorageAccountName $Account.StorageAccountName - ResourceGroupName $Account.ResourceGroupName
322- if ($shouldDelete ) {
323- $deleteNow += $blob
319+ foreach ($container in $containers ) {
320+ $blobs = $container | Get-AzStorageBlob
321+ foreach ($blob in $blobs ) {
322+ $shouldDelete = EnableBlobDeletion - Blob $blob - Container $container - StorageAccountName $Account.StorageAccountName - ResourceGroupName $Account.ResourceGroupName
323+ if ($shouldDelete ) {
324+ $deleteNow += $blob
325+ }
324326 }
325327 }
326328 } catch {
@@ -342,11 +344,15 @@ function RemoveStorageAccount($Account) {
342344 }
343345
344346 foreach ($container in $containers ) {
347+ if (! ($container | Get-Member ' BlobContainerProperties' )) {
348+ continue
349+ }
345350 if ($container.BlobContainerProperties.HasImmutableStorageWithVersioning ) {
346351 try {
347352 # Use AzRm cmdlet as deletion will only work through ARM with the immutability policies defined on the blobs
348- Remove-AzRmStorageContainer - Name $container.Name - StorageAccountName $Account.StorageAccountName - ResourceGroupName $Account.ResourceGroupName - Force
349- # $container | Remove-AzStorageContainer
353+ # Add a retry in case blob deletion has not finished in time for container deletion, but not too many that we end up
354+ # getting throttled by ARM/SRP if things are actually in a stuck state
355+ Retry - Attempts 1 - Action { Remove-AzRmStorageContainer - Name $container.Name - StorageAccountName $Account.StorageAccountName - ResourceGroupName $Account.ResourceGroupName - Force }
350356 } catch {
351357 Write-Host " Container removal failed: $ ( $container.Name ) , account: $ ( $Account.storageAccountName ) , group: $ ( $Account.ResourceGroupName ) "
352358 Write-Warning " Ignoring the error and trying to delete the storage account"
@@ -360,7 +366,7 @@ function RemoveStorageAccount($Account) {
360366 }
361367}
362368
363- function EnableBlobDeletion ($Blob , $StorageAccountName , $ResourceGroupName ) {
369+ function EnableBlobDeletion ($Blob , $Container , $ StorageAccountName, $ResourceGroupName ) {
364370 # Some properties like immutability policies require the blob to be
365371 # deleted before the container can be deleted
366372 $forceBlobDeletion = $false
@@ -394,6 +400,10 @@ function EnableBlobDeletion($Blob, $StorageAccountName, $ResourceGroupName) {
394400 $Blob.ICloudBlob.BreakLease ()
395401 }
396402
403+ if (($Container | Get-Member ' BlobContainerProperties' ) -and $Container.BlobContainerProperties.HasImmutableStorageWithVersioning ) {
404+ $forceBlobDeletion = $true
405+ }
406+
397407 return $forceBlobDeletion
398408}
399409
0 commit comments