Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions eng/common/scripts/Helpers/Resource-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ filter Remove-PurgeableResources {
switch ($r.AzsdkResourceType) {
'Key Vault' {
if ($r.EnablePurgeProtection) {
# We will try anyway but will ignore errors.
Write-Warning "Key Vault '$($r.VaultName)' has purge protection enabled and may not be purged until $($r.ScheduledPurgeDate)"
continue
}

# Use `-AsJob` to start a lightweight, cancellable job and pass to `Wait-PurgeableResoruceJob` for consistent behavior.
Expand All @@ -134,8 +134,8 @@ filter Remove-PurgeableResources {

'Managed HSM' {
if ($r.EnablePurgeProtection) {
# We will try anyway but will ignore errors.
Write-Warning "Managed HSM '$($r.Name)' has purge protection enabled and may not be purged until $($r.ScheduledPurgeDate)"
continue
}

# Use `GetNewClosure()` on the `-Action` ScriptBlock to make sure variables are captured.
Expand Down Expand Up @@ -345,8 +345,9 @@ function RemoveStorageAccount($Account) {
if ($container.BlobContainerProperties.HasImmutableStorageWithVersioning) {
try {
# Use AzRm cmdlet as deletion will only work through ARM with the immutability policies defined on the blobs
Remove-AzRmStorageContainer -Name $container.Name -StorageAccountName $Account.StorageAccountName -ResourceGroupName $Account.ResourceGroupName -Force
#$container | Remove-AzStorageContainer
# Add a retry in case blob deletion has not finished in time for container deletion, but not too many that we end up
# getting throttled by ARM/SRP if things are actually in a stuck state
Retry -Attempts 1 -Action { Remove-AzRmStorageContainer -Name $container.Name -StorageAccountName $Account.StorageAccountName -ResourceGroupName $Account.ResourceGroupName -Force }
} catch {
Write-Host "Container removal failed: $($container.Name), account: $($Account.storageAccountName), group: $($Account.ResourceGroupName)"
Write-Warning "Ignoring the error and trying to delete the storage account"
Expand All @@ -360,7 +361,7 @@ function RemoveStorageAccount($Account) {
}
}

function EnableBlobDeletion($Blob, $StorageAccountName, $ResourceGroupName) {
function EnableBlobDeletion($Blob, $Container, $StorageAccountName, $ResourceGroupName) {
# Some properties like immutability policies require the blob to be
# deleted before the container can be deleted
$forceBlobDeletion = $false
Expand Down Expand Up @@ -394,6 +395,10 @@ function EnableBlobDeletion($Blob, $StorageAccountName, $ResourceGroupName) {
$Blob.ICloudBlob.BreakLease()
}

if ($container.BlobContainerProperties.HasImmutableStorageWithVersioning) {
$forceBlobDeletion = $true
}

return $forceBlobDeletion
}

Expand Down
14 changes: 13 additions & 1 deletion eng/scripts/live-test-resource-cleanup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,12 @@ function DeleteArmDeployments([object]$ResourceGroup) {
$null = $toDelete | Remove-AzResourceGroupDeployment
}

function DeleteSubscriptionDeployments() {
$subDeployments = Get-AzSubscriptionDeployment
Write-Host "Removing $($subDeployments.Count) subscription scoped deployments"
$subDeployments | Remove-AzSubscriptionDeployment
}

function DeleteOrUpdateResourceGroups() {
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
param()
Expand Down Expand Up @@ -403,7 +409,12 @@ function DeleteOrUpdateResourceGroups() {
$hasError = DeleteAndPurgeGroups $toDelete

foreach ($rg in $toClean) {
DeleteArmDeployments $rg
try {
DeleteArmDeployments $rg
} catch {
Write-Warning "Error deleting deployments for group '$($rg.ResourceGroupName)'"
Write-Warning $_
}
}

if ($hasError) {
Expand Down Expand Up @@ -507,6 +518,7 @@ if ($SubscriptionId -and ($originalSubscription -ne $SubscriptionId)) {

try {
DeleteOrUpdateResourceGroups
#DeleteSubscriptionDeployments
} finally {
if ($SubscriptionId -and ($originalSubscription -ne $SubscriptionId)) {
Select-AzSubscription -Subscription $originalSubscription -Confirm:$false -WhatIf:$false
Expand Down
Loading