Scriptrunner Bulk Remove Space

Norm Moore
Contributor
January 23, 2023

Hi All,

I'm working with the following script in the Script Console on Confluence to remove spaces. I've been able to test this code and it works to remove spaces on an individual basis, but I'm not that great at modifying groovy script "YET" 

I was wondering if anyone else has used something similar to bulk remove several sites at once? 

The biggest issue I'm encountering is not being able to add multiple space keys to the script. I'm not sure of which separator to add between space keys.

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.spaces.SpaceManager


def spaceManager = ComponentLocator.getComponent(SpaceManager)

def spaceKeyToDelete = "G1507"

def allSpaces = spaceManager.getAllSpaces()

allSpaces.each{
def currSpace = it
def currSpaceKey = it.getKey()
if(currSpaceKey == spaceKeyToDelete){
spaceManager.removeSpace(currSpace)
log.warn("Removed space with key =" + currSpaceKey)
}
}

2 answers

1 accepted

0 votes
Answer accepted
Tiffany Wortham
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.
April 7, 2023

Hi Norm!

You can remove several spaces at once with this script:

import com.atlassian.confluence.api.model.Expansion
import com.atlassian.confluence.api.model.pagination.SimplePageRequest
import com.atlassian.confluence.api.service.content.SpaceService
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)

def spaceResponse = spaceService.find(new Expansion('name'))
.withKeys("SPACEKEY1", "SPACEKEY2", "SPACEKEY3") //replace with your space keys
.fetchMany(new SimplePageRequest(0, 10)) //adjust from 10 to however many results you want

if (spaceResponse) {
def spaces = spaceResponse.getResults()
spaces.each { space ->
spaceService.delete(space)
log.warn("Removed space with key $space.key")
}
} else {
log.warn("No spaces found")
}

Let me know if this works for you (:

Tiffany

Norm Moore
Contributor
September 25, 2023

Hi @Tiffany Wortham 

I was curious to know if your script would work with "category" in place of the space key?

I have  A LOT of spaces that are grossly outdated and not being used, I'd like to remove them, but going space key by space key seems like it would take quite a while to complete. 

 

Thanks!!

Like Manuel Sollbach likes this
0 votes
Stephane Vitry
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 12, 2023

I had to do the same thing but in a test sandbox in Confluence cloud.
I ended up using this groovy script in Scriptrunner which worked.

def response = get('/wiki/rest/api/space').asObject(Map)
def spaceKeys = response.body.results.key

spaceKeys.each { spaceKey ->
def result = delete("/wiki/rest/api/space/${spaceKey}")
.asString()

if (result.status == 404) {
logger.warn("Space with key: {} does not exist", spaceKey)
} else if (result.status == 202){
logger.info("Space with key: {} has been deleted. Response code: {}. Response body: {}", spaceKey, result.status, result.body)
} else {
logger.warn("Failed to delete space with key: {}. Response code: {}. Response body: {}", spaceKey, result.status, result.body)
}
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events