Scriptrunner Bulk Remove Space
Asked by Norm Moore:

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

<br />

**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)**   
**}**   
**}**


Answers:
author: Tiffany Wortham
body: 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

comments: author: Norm Moore
body: Hi [@Tiffany Wortham](/t5/user/viewprofilepage/user-id/1420833)

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


author: Stephane Vitry
body: 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)
> }
> }
> ```


Tags: confluence-server

data-center

server