Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Scriptrunner Bulk Remove Space

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.
Apr 07, 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

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!!

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