@@ -7,7 +7,9 @@ function Remove-ConfluencePage {
77 . DESCRIPTION
88 Moves a page to the trash. With -Recurse, direct and nested child pages
99 are removed first so that a whole subtree can be deleted. With -Purge the
10- page is permanently deleted (only valid for already-trashed pages).
10+ page is permanently deleted: because Confluence requires a page to be in
11+ the trash before it can be purged, the page is trashed first and then
12+ purged in a second call.
1113
1214 . EXAMPLE
1315 ```powershell
@@ -31,7 +33,8 @@ function Remove-ConfluencePage {
3133 # Remove child pages before removing the page itself.
3234 [switch ]$Recurse ,
3335
34- # Permanently delete instead of moving to trash.
36+ # Permanently delete the page. It is moved to the trash first (as the API
37+ # requires) and then purged.
3538 [switch ]$Purge ,
3639
3740 # The context to use: an object, a context name, or $null for the default.
@@ -45,12 +48,21 @@ function Remove-ConfluencePage {
4548 }
4649 }
4750
48- $endpoint = " /wiki/api/v2/pages/$PageId "
49- if ($Purge ) {
50- $endpoint = ' {0}?purge=true' -f $endpoint
51- }
51+ $base = " /wiki/api/v2/pages/$PageId "
52+ $action = if ($Purge ) { ' Permanently delete Confluence page' } else { ' Delete Confluence page' }
5253
53- if ($PSCmdlet.ShouldProcess ($PageId , ' Delete Confluence page' )) {
54- Invoke-ConfluenceRestMethod - ApiEndpoint $endpoint - Method ' DELETE' - Context $Context
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 {
65+ Invoke-ConfluenceRestMethod - ApiEndpoint $base - Method ' DELETE' - Context $Context
66+ }
5567 }
5668}
0 commit comments