ScriptRunner for Confluence selective bulk deletion of pages
Asked by Gordon Rutherford:

Hi

We have recently purchased SR for Confluence and as a non coder I am looking for a script that could help with the bulk deletion of 'selected pages' to help in housekeeping our DC instance.

The selection would be something like 'last updated more than 2 years ago' and then the ability to bulk delete that selection.

A related but side question on the selection - could SR establish a 'last page view'?

Then I could combine these two filters or run them independently before deleting the returned pages

Thanks in advance for help here!

Regards

Gordon


Answers:
author: Jessie Wang_ScriptRunner_The Adaptavist Group
body: Hi all, this script is now available in our Script Library for ScriptRunner for Confluence Server/DC (tested by our engineers).

Feel free to copy or customise it as you wish <https://library.adaptavist.com/entity/automate-the-removal-of-old-or-inactive-content>

comments: author: Gordon Rutherford
body: Nice - thanks for update


author: Tiffany Wortham
body: Hey [@Gordon Rutherford](/t5/user/viewprofilepage/user-id/62271), did you ever get solution? If not, I may be able to come up with something.

comments: author: Gordon Rutherford
body: Hi Tiffany

Not yet and I am actually getting a price from the Adaptavist Scripting service to write the code - and if it is not to expensive for my limited budget I would proceed with that. I have received a lot of helpful (free) support from your team over the last two years and would be happy to pay for it for once if price is reasonable!

If not I may be raising it out the next champions hour!

Regards

Gordon


author: Tiffany Wortham
body: Hey Gordon!  

Since I didn't get to answer this for Champion Hour, here's a solution that may work for you.  

Create a CQL escalation service job that looks like this:  
![Screen Shot 2022-02-22 at 12.19.50 PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/184851i298A97D9ECD74377/image-size/large?v=v2&px=999 "Screen Shot 2022-02-22 at 12.19.50 PM.png")  
It may seem like a strange choice, but we're using a CQL escalation service job so we can use CQL to select the right pages. I have the cron expression set so far in the future so that it won't run regularly, but if you would like it to run it regularly, of course, you could just use a different cron expression. Since we're selecting the pages through CQL, the script is really simple:  

```
import com.atlassian.confluence.core.DefaultDeleteContext
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator

def pageManager = ComponentLocator.getComponent(PageManager)

hits.each { page ->
    pageManager.trashPage(page, DefaultDeleteContext.DEFAULT)
}
```

In order to see which pages you're about to delete, you can check the hits you get next to the CQL Query field. If you see some pages you don't want to delete, just add a label to them and exclude that label in your CQL query, like "label != 'whatever'"  

You can just hit "Run now" to run this once. Hope this helps (:


author: Gordon Rutherford
body: Hi Tiffany

I really appreciate you coming back to me with this after the hour yesterday which, as always was great.

Something is not quite working as expected for me and it throws a StackOverflowError when I 'run now', however if you then run it again it works as expected and deletes the page. See my snip for the error

Thanks for your input

Best regards

Gordon![stackoevrflow.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/185050i7085AE7AF22027D5/image-size/large?v=v2&px=999 "stackoevrflow.PNG")


author: Tiffany Wortham
body: Hi Gordon,  

I think I remember this happening to me at one point too. I'm not sure why this happens sometimes, but I don't think it actually has anything to do with the script itself. I'd say just ignore it, and try running it again when that happens.


author: Gordon Rutherford
body: Hi Tiffany

Thanks for coming back to me and yes ignoring error seems to be fine

Cheers

Gordon


author: Fabian Lim
body: Adding [@Ravi Sagar _Sparxsys_](/t5/user/viewprofilepage/user-id/853596)

Maybe he can shoot a tutorial on his youtube vlogs.


Tags: confluence-server

data-center

scriptrunner

server