Skip to content

Commit 2885fe2

Browse files
committed
add filter for stacks
1 parent 5a27564 commit 2885fe2

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

src/ALZ/Public/Remove-PlatformLandingZone.ps1

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ function Remove-PlatformLandingZone {
128128
containing "Custom" anywhere in their name).
129129
Default: Empty array (delete all custom role definitions)
130130
131+
.PARAMETER DeploymentStacksToDeleteNamePatterns
132+
An array of wildcard patterns for deployment stack names that should be deleted. Only deployment stacks
133+
matching any of these patterns will be deleted during the deployment stack cleanup process. If the array
134+
is empty, all deployment stacks will be deleted (default behavior). Each pattern is evaluated using a
135+
-like expression with wildcards at the start and end (e.g., a pattern of "alz" will match deployment stacks
136+
containing "alz" anywhere in their name).
137+
Default: Empty array (delete all deployment stacks)
138+
131139
.EXAMPLE
132140
Remove-PlatformLandingZone -ManagementGroups @("alz-platform", "alz-landingzones")
133141
@@ -220,6 +228,12 @@ function Remove-PlatformLandingZone {
220228
Removes management groups and resource groups but only deletes custom role definitions with names containing
221229
"Test-Role" or "Temporary". Useful when you want to clean up specific custom roles while preserving others.
222230
231+
.EXAMPLE
232+
Remove-PlatformLandingZone -ManagementGroups @("alz-test") -DeploymentStacksToDeleteNamePatterns @("alz-", "test-")
233+
234+
Removes management groups and resource groups but only deletes deployment stacks with names containing
235+
"alz-" or "test-". Useful when you want to clean up specific deployment stacks while preserving others.
236+
223237
.NOTES
224238
This function uses Azure CLI commands and requires:
225239
- Azure CLI to be installed and available in the system path
@@ -283,7 +297,8 @@ function Remove-PlatformLandingZone {
283297
[switch]$SkipOrphanedRoleAssignmentDeletion,
284298
[switch]$SkipCustomRoleDefinitionDeletion,
285299
[string[]]$ManagementGroupsToDeleteNamePatterns = @(),
286-
[string[]]$RoleDefinitionsToDeleteNamePatterns = @()
300+
[string[]]$RoleDefinitionsToDeleteNamePatterns = @(),
301+
[string[]]$DeploymentStacksToDeleteNamePatterns = @()
287302
)
288303

289304
function Write-ToConsoleLog {
@@ -573,7 +588,8 @@ function Remove-PlatformLandingZone {
573588
[switch]$PlanMode,
574589
[string]$TempLogFileForPlan,
575590
[switch]$SkipDeploymentStackDeletion,
576-
[switch]$SkipDeploymentDeletion
591+
[switch]$SkipDeploymentDeletion,
592+
[string[]]$DeploymentStacksToDeleteNamePatterns = @()
577593
)
578594

579595
if(-not $PSCmdlet.ShouldProcess("Delete Deployments", "delete")) {
@@ -594,6 +610,27 @@ function Remove-PlatformLandingZone {
594610
$deploymentStacks = (az stack mg list --management-group-id $ScopeId --query "[].{name:name,id:id}" -o json 2>$null) | ConvertFrom-Json
595611
}
596612

613+
# Filter deployment stacks to only include those matching deletion patterns
614+
if ($DeploymentStacksToDeleteNamePatterns -and $DeploymentStacksToDeleteNamePatterns.Count -gt 0) {
615+
$filteredDeploymentStacks = @()
616+
foreach($stack in $deploymentStacks) {
617+
$shouldDelete = $false
618+
foreach($pattern in $DeploymentStacksToDeleteNamePatterns) {
619+
if($stack.name -like "*$pattern*") {
620+
Write-ToConsoleLog "Including deployment stack for deletion due to pattern match '$pattern': $($stack.name)" -NoNewLine
621+
$shouldDelete = $true
622+
break
623+
}
624+
}
625+
if($shouldDelete) {
626+
$filteredDeploymentStacks += $stack
627+
} else {
628+
Write-ToConsoleLog "Skipping deployment stack (no pattern match): $($stack.name)" -NoNewLine
629+
}
630+
}
631+
$deploymentStacks = $filteredDeploymentStacks
632+
}
633+
597634
if ($deploymentStacks -and $deploymentStacks.Count -gt 0) {
598635
Write-ToConsoleLog "Found $($deploymentStacks.Count) deployment stack(s) in $($ScopeType): $ScopeNameForLogs" -NoNewLine
599636

@@ -1060,7 +1097,8 @@ function Remove-PlatformLandingZone {
10601097
-PlanMode:$using:PlanMode `
10611098
-TempLogFileForPlan $using:TempLogFileForPlan `
10621099
-SkipDeploymentStackDeletion:$using:SkipDeploymentStackDeletion `
1063-
-SkipDeploymentDeletion:$using:SkipDeploymentDeletion
1100+
-SkipDeploymentDeletion:$using:SkipDeploymentDeletion `
1101+
-DeploymentStacksToDeleteNamePatterns $using:DeploymentStacksToDeleteNamePatterns
10641102

10651103
} -ThrottleLimit $ThrottleLimit
10661104
} else {
@@ -1263,7 +1301,8 @@ function Remove-PlatformLandingZone {
12631301
-PlanMode:$using:PlanMode `
12641302
-TempLogFileForPlan $using:TempLogFileForPlan `
12651303
-SkipDeploymentStackDeletion:$using:SkipDeploymentStackDeletion `
1266-
-SkipDeploymentDeletion:$using:SkipDeploymentDeletion
1304+
-SkipDeploymentDeletion:$using:SkipDeploymentDeletion `
1305+
-DeploymentStacksToDeleteNamePatterns $using:DeploymentStacksToDeleteNamePatterns
12671306
} else {
12681307
Write-ToConsoleLog "Skipping subscription level deployment and deployment stack deletion in subscription: $($subscription.Name) (ID: $($subscription.Id))" -NoNewLine
12691308
}

0 commit comments

Comments
 (0)