Skip to main content
Book your demo
Example scripts
To homepage
New
Confluence
Cloud icon
Cloud

Find and Replace Text Across Space

Created 1 month(s) ago, Updated 6 day(s) ago
App in script
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
Compatibility
compatibility bullet
Confluence
Language |
groovy
def spaceKey = 'ans'
def oldText = 'test'
def newText = 'page'

def pages = Spaces.getByKey(spaceKey).getAllPages(){
    setBodyFormat("storage")
}

pages.findAll { page ->
    def storage = page.body.storage.value
    def newContent = storage.replace(oldText, newText) // case sensitive replace

    if (newContent == storage) {
        return false
    }

    logger.info("Updating page '${page.title}' with new body")
    page.update(){
        title = page.title
        status = "current"
        body = newContent
    }
    logger.info("Page '${page.title}' has been updated.")
    return true
}.collect { it.id }
Having an issue with this script?
Report it here