Using CLI's renderRequest to bulk restore deleted pages on Confluence

So yesterday a user accidentally deleted a pagetree containing 160 pages. The question was, how to restore all of those pages without having to click "Restore" and "OK" 160 times.

giphy

The answer? The venerable @Bob Swift _Appfire_'s Confluence CLI!

How to restore a single page with CLI

CLI does not have a function to restore a deleted page, but if you look at a confirmation page's source (example: https://YOURATLASSIANSITE/pages/restoretrashitem.action?key=MS&contentId=139634347), you'll see it is just making a POST request. Reading HTML source and finding POST requests is an old-school web stuff, which thankfully Atlassian has chosen to not make overly complicated with Javascript and things I don't understand. I'll include some resources about this at the end.

Endpoint

https://YOURCONFLUENCESITE/pages/dorestoretrashitem.action

Parameters

  • key
  • contentID

CLI has an incredibly useful action called renderRequest which can make that POST request using the same credentials you are using with CLI. It's kind of like using curl but without having to mess with any authentication stuff.

The full command looks like this:

acli wiki --action renderRequest --request "/pages/dorestoretrashitem.action" --requestType POST --type FORM_URL_ENCODED --requestParameters="key=MS&contentId=139634347" 

That command would restore the single page in the MS space with an ID of 139634347.

Restore ALL the pages with CLI

So we've figured out how to use renderRequest to replace a single page. But as you might recall our user accidentally deleted 160 pages. We want to restore all of those.

For that, we will use CLI's runFromTrashList allows you to iterate through every single page in the Trash, and run a given action against each one. In our case, it will be the renderRequest action that does the restore.

Here is the command: 

acli wiki --action runFromTrashList --space MS --common '-a renderRequest --request "/pages/dorestoretrashitem.action" --requestType POST --type FORM_URL_ENCODED --requestParameters=key=MS&contentId=@contentId@'

So then, we are looking for all trashed pages in the MS space, and running the same renderRequest command, replacing contentId with each page in turn. 

Sharp-eyed readers will notice that the requestParameters are not double-quoted in the this command. It's... a quoting thing. I don't fully understand it, but at least on a Mac, it seems to have worked. You may need to play around with different quoting mechanisms if you run into problems, which leads us to this last bit:

You really should TEST THIS ON A DEVELOPMENT SYSTEM before trying it on Production. (Although this is a restore operation, so the worst thing that happens is you don't restore the page and have to go and figure out what went wrong.)

Sidenotes

Hierarchy

One bummer about restoring pages from the trash is that they end up at the root level of the space. As the documentation advises, you or the user will need to go to Space Tools > Reorder Pages to drag all of the restored pages back into the hierarchy, which yeah, will not be fun with 160 pages.

Waaay back in 2018, @Jonathan Smith suggested:

you could also just view the hierarchy from DEV and recreate the structure in PROD.

That's good advice, if you have a DEV instance handy.

Tested on Server and Cloud

I tested this on Confluence Server and Cloud, and was happy to see that it worked on both platforms! Yay for Atlassian not changing this mechanism when they moved it to the Cloud.

Also - I just gotta say again, thank you to @Bob Swift _Appfire_ for the truly amazing tool he and his team have created.

References

6 comments

Andy Gladstone
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 22, 2023

@Darryl Lee thanks for this article and for turning a user mistake into a learning experience.

Like # people like this
Amelie Winkler _Appfire_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 24, 2023

Hi @Darryl Lee – Amelie from Appfire's product team here. Thanks for sharing this inspiring use case!

We'd love to learn more about how you're using the CLI. Would you be open to jumping on a quick call to chat about it? 

Cheers, Amelie

Like Dave Liao likes this
Dave Liao
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 4, 2023

@Darryl Lee - CLI to the rescue! 🙌

I remember useing CLI for Jira back in the day to instantiate a bunch of accounts. Such a glorious timesaver.

I now have CLI for Jira Cloud on one of my production Cloud instances. Because it's awesome.

Like # people like this
Jonathan Smith
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 7, 2023

@Darryl Lee I don't know if my advice was good, but it was the only thing available at the time :)

Like Darryl Lee likes this
Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 7, 2023

Sadly @Jonathan Smith it's still the only available option.

Like Andy Gladstone likes this
WW
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 26, 2023

<whew>

Thank you so much for posting this!

I was cleaning up a lower environment and some pages are messed up, for whatever reason, and won't purge from the space trash. So that I can delete all the spaces with messed up pages, I decided to move them to one centralized, messed up space.

I was able to write a ScriptRunner script to delete the pages (space delete failed bc of problem pages) and attempt to purge the pages. But because the problem pages only show themselves when trying to purge, I'm stuck with a bunch of pages in the trash. I get an error and it rolls back whenever I try to restore with ScriptRunner. (I used ScriptRunner because I wasn't deleting all of the spaces and needed to do some things that were fancier than the CLI can handle.)

I still need to either bulk put the pages under a parent page so I can easily move them or bulk move them some other way, but that should be ok.

This will also be helpful for other things with POST and .action that are done in Confluence.

Thank you, thank you!

Like Darryl Lee likes this

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events