Skip to content

Commit 8b717c2

Browse files
🩹 [Patch]: Preserve page status on update; trash-then-purge for Remove-ConfluencePage -Purge
1 parent e282457 commit 8b717c2

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

‎src/functions/public/Pages/Set-ConfluencePage.ps1‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ function Set-ConfluencePage {
3939
[ValidateSet('storage', 'atlas_doc_format', 'wiki')]
4040
[string]$Representation = 'storage',
4141

42-
# The page status. Defaults to 'current'.
43-
[string]$Status = 'current',
42+
# The page status. Defaults to the page's current status.
43+
[string]$Status,
4444

4545
# The context to use: an object, a context name, or $null for the default.
4646
[object]$Context
@@ -50,10 +50,11 @@ function Set-ConfluencePage {
5050

5151
$newTitle = if ($PSBoundParameters.ContainsKey('Title')) { $Title } else { $current.title }
5252
$newBody = if ($PSBoundParameters.ContainsKey('Body')) { $Body } else { $current.body.$Representation.value }
53+
$newStatus = if ($PSBoundParameters.ContainsKey('Status')) { $Status } else { $current.status }
5354

5455
$payload = @{
5556
id = $PageId
56-
status = $Status
57+
status = $newStatus
5758
title = $newTitle
5859
version = @{
5960
number = [int]$current.version.number + 1

0 commit comments

Comments
 (0)