Skip to content

Commit 7e225b1

Browse files
Confirm root before recursing in Remove-ConfluencePage
With -Recurse, child pages were deleted before the root page's ShouldProcess was evaluated, so declining the root -Confirm prompt (after confirming children) left a partially deleted subtree. Evaluate the root ShouldProcess first and only recurse once approved (suppressing the per-child prompt), while -WhatIf still enumerates the whole subtree. Verified the WhatIf/Confirm/normal control flow (review thread on Remove-ConfluencePage L67).
1 parent f22d268 commit 7e225b1

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

src/functions/public/Pages/Remove-ConfluencePage.ps1

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,41 @@ function Remove-ConfluencePage {
4141
[object]$Context
4242
)
4343

44+
$base = "/wiki/api/v2/pages/$PageId"
45+
$action = if ($Purge) { 'Permanently delete Confluence page' } else { 'Delete Confluence page' }
46+
47+
# Confirm the root deletion up front so that declining -Confirm never leaves a partially
48+
# deleted subtree. ShouldProcess returns $false for both -WhatIf and a declined prompt; a
49+
# -WhatIf run should still enumerate the whole subtree, so only stop early on a real decline.
50+
$approved = $PSCmdlet.ShouldProcess($PageId, $action)
51+
if (-not $approved -and -not $WhatIfPreference) {
52+
return
53+
}
54+
4455
if ($Recurse) {
56+
# Children are removed before the parent (required by -Purge). The root is already
57+
# confirmed, so the per-child prompt is suppressed; -WhatIf still propagates so a dry
58+
# run lists the entire subtree.
4559
$children = Get-ConfluencePageChild -PageId $PageId -Context $Context
4660
foreach ($child in $children) {
47-
Remove-ConfluencePage -PageId $child.id -Recurse -Purge:$Purge -Context $Context
61+
Remove-ConfluencePage -PageId $child.id -Recurse -Purge:$Purge -Context $Context -Confirm:$false
4862
}
4963
}
5064

51-
$base = "/wiki/api/v2/pages/$PageId"
52-
$action = if ($Purge) { 'Permanently delete Confluence page' } else { 'Delete Confluence page' }
65+
if (-not $approved) {
66+
return
67+
}
5368

54-
if ($PSCmdlet.ShouldProcess($PageId, $action)) {
55-
if ($Purge) {
56-
# A page must be in the trash before it can be purged. Trash first
57-
# (ignoring failures, e.g. when it is already trashed), then purge.
58-
try {
59-
Invoke-ConfluenceRestMethod -ApiEndpoint $base -Method 'DELETE' -Context $Context
60-
} catch {
61-
Write-Verbose "Trash step before purge failed (the page may already be trashed): $($_.Exception.Message)"
62-
}
63-
Invoke-ConfluenceRestMethod -ApiEndpoint ('{0}?purge=true' -f $base) -Method 'DELETE' -Context $Context
64-
} else {
69+
if ($Purge) {
70+
# A page must be in the trash before it can be purged. Trash first
71+
# (ignoring failures, e.g. when it is already trashed), then purge.
72+
try {
6573
Invoke-ConfluenceRestMethod -ApiEndpoint $base -Method 'DELETE' -Context $Context
74+
} catch {
75+
Write-Verbose "Trash step before purge failed (the page may already be trashed): $($_.Exception.Message)"
6676
}
77+
Invoke-ConfluenceRestMethod -ApiEndpoint ('{0}?purge=true' -f $base) -Method 'DELETE' -Context $Context
78+
} else {
79+
Invoke-ConfluenceRestMethod -ApiEndpoint $base -Method 'DELETE' -Context $Context
6780
}
6881
}

0 commit comments

Comments
 (0)