Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,290
Community Members
 
Community Events
165
Community Groups

Looking to automate deleting pages with a specific label...

Hello - we are running (server) Confluence with Comala Document Management and Scriptrunner.

There is an archival workflow that manages pages such that after a year of inactivity they are flagged for review and if no action is taken they are then flagged for removal.

Comala Document Management does not have the capability to delete pages.  The most I can do is restrict them and add a label "to-be-deleted". 

Scriptrunner appears to have to the theoretical ability to run a script, on a schedule, that will look for all pages with a specific label (i.e. to-be-deleted) and delete said pages.

I have a small script in Scriptrunner that will find and return the ids of all pages with a specific label - what I have not been able to find is how to delete pages in a Scriptrunner script.

1 answer

1 accepted

Hi Jason! 

It sounds like you're almost there with your script so I'll just add the snippet displaying how you can send a single page to the trash. 

The Page Manager contains the method Trash Page which will accept the ids of the page you have already fetched as well as a Delete Context.  

You can view an example of how this is used below:

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


def pageManager = ComponentLocator.getComponent(PageManager)
def pageToDelete = pageManager.getPage(<page_id>)

pageManager.trashPage(pageToDelete, DefaultDeleteContext.DEFAULT)

Kind regards,  
Lee

Lee - as I never said at the time, thanks for this snippet of code.

Moving on - Wow.  Blast from the past.  This got back-burnered for a long time, as much due to rage-quitting as anything else.

I am stuck at not being able to find a code permutation that will allow for anything other than deleting a page with a specific value for page_id.

My code as this point is:

 

import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator

def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)
String label = "to-be-deleted"
def ids = []

for (Space space : spaceManager.getAllSpaces()) {
    for (Page page : pageManager.getPages(space, true)) {
        for (Label pageLabel : page.getLabels()) {
            if (pageLabel.getName().equalsIgnoreCase(label)) {
                ids << String.valueOf(page.getId())
            }
        }
    }
}
import com.atlassian.confluence.core.DefaultDeleteContext
for (Space space : spaceManager.getAllSpaces()) {
    for (Page page : pageManager.getPages(space, true)) {
        for (int i = 0; i < ids.size(); i++) {
            def pageToDeleteID = ids[i]
            def pageToDelete = pageManager.getPage(pageToDeleteID)
            pageManager.trashPage(pageToDelete, DefaultDeleteContext.DEFAULT)
        }
    }
}
The above code will work if I insert a specific value 123456 in the place of pageToDeleteID.  It does not work as currently constituted - the error given in scriptrunner console is "[Static type checking] - Cannot find matching method com.atlassian.confluence.pages.PageManager#getPage(E)"

Hi Jason!!

Wow, it sure has been a minute since we last spoke about this! I hope you're doing well ๐Ÿ™‚

I believe I've found a pretty efficient way of handling this problem. 

Here's the script:

import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.labels.LabelManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.DefaultDeleteContext
import com.atlassian.confluence.core.ContentEntityObject
import com.atlassian.confluence.pages.AbstractPage

def pageManager = ComponentLocator.getComponent(PageManager)
def labelManager = ComponentLocator.getComponent(LabelManager)

Label label = new Label("delete")
def labeledPages = labelManager.getContentForLabel(0,50,label).getList()

labeledPages.each{ ContentEntityObject page ->
if(page.getClass() == Page){
pageManager.trashPage(page as AbstractPage, DefaultDeleteContext.DEFAULT)
}

 


To give you a rundown of what's happening here, we're using Confluence's in-built getContentForLabel method to fetch all content, i.e. blog pages, space categories and Pages, that have a specified label.

I've set the number of pages to fetch as 50, but feel free to change this. I'd also recommend taking a good look at the Label Manager, as you may find a more suitable method for your particular use case there. 

Next up, we're looping through the content returned with that specific label, checking that it's a Page, then moving it to the trash!

As a note, I'd recommend running this on a test instance and ensuring that it is working as expected before running it on production.

 

I hope this helps, but please let me know if you have any further questions ๐Ÿ˜

This worked beautifully - thank you Lee.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.3.1
TAGS
Community showcase
Published in Confluence

Confluence: Where work and wellness meet

Feeling overwhelmed by the demands of work and life? With a 25% increase in the prevalence of anxiety and depression worldwide during the pandemic, for most of us, itโ€™s a resounding yes . ๐Ÿ™‹โ€โ™€๏ธ ...

731 views 5 21
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you