Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly handle legal holds when cleaning up storage accounts #6605

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Changes from all 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
16 changes: 16 additions & 0 deletions eng/scripts/Remove-WormStorageAccounts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ param(
[string]$GroupPrefix
)

$ErrorActionPreference = 'Stop'

# Be a little defensive so we don't delete non-live test groups via naming convention
if (!$groupPrefix -or !$GroupPrefix.StartsWith('rg-')) {
Write-Error "The -GroupPrefix parameter must start with 'rg-'"
Expand All @@ -25,6 +27,20 @@ foreach ($group in $groups) {
Write-Host "Removing $($account.StorageAccountName) in $($account.ResourceGroupName)"
}
$ctx = New-AzStorageContext -StorageAccountName $account.StorageAccountName
$immutableBlobs = $ctx `
| Get-AzStorageContainer `
| Where-Object { $_.BlobContainerProperties.HasImmutableStorageWithVersioning } `
| Get-AzStorageBlob
try {
foreach ($blob in $immutableBlobs) {
Write-Host "Removing legal hold - blob: $($blob.Name), account: $($account.StorageAccountName), group: $($group.ResourceGroupName)"
$blob | Set-AzStorageBlobLegalHold -DisableLegalHold | Out-Null
}
} catch {
Write-Warning "User must have 'Storage Blob Data Owner' RBAC permission on subscription or resource group"
Write-Error $_
throw
}
$ctx | Get-AzStorageContainer | Get-AzStorageBlob | Remove-AzStorageBlob -Force
# Use AzRm cmdlet as deletion will only work through ARM with the immutability policies defined on the blobs
$ctx | Get-AzStorageContainer | % { Remove-AzRmStorageContainer -Name $_.Name -StorageAccountName $ctx.StorageAccountName -ResourceGroupName $group.ResourceGroupName -Force }
Expand Down