Skip to main content
Example scripts
arrow icon
To homepage
Confluence
Cloud icon
Cloud

Remove group access from all spaces

Created 10 month(s) ago, Updated 14 day(s) ago
App in script
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
Compatibility
compatibility bullet
Confluence
Language |
groovy
import com.atlassian.confluence.rest.clientv1.model.GroupArrayWithLinks
import com.atlassian.confluence.rest.clientv2.model.*

def inputGroupName = "INPUT_GROUP_NAME"
String username = "USERNAME"
String token = "BASIC_AUTH_TOKEN"

def groups = get('/wiki/rest/api/group').asObject(GroupArrayWithLinks).body
if (!groups) {
    throw new RuntimeException("Failed to fetch groups.")
}
def groupId = groups.results.find { it.name == inputGroupName }?.id ?:
        logger.error("Group $inputGroupName not exist")

def removedSpaceKeys = []
def spaces = getAllItems("/wiki/api/v2/spaces", MultiEntityResultSpace)
spaces.each { Space space ->
    def permissions = getAllItems("/wiki/api/v2/spaces/${space.id}/permissions", MultiEntityResultSpacePermission)
    permissions.each { SpacePermission permission ->
        if (permission.principal.id == groupId) {
            def response = delete("${baseUrl}/rest/api/space/${space.key}/permission/${permission.id}")
                    .basicAuth(username, token)
                    .asObject(Map)
            if (response.status != 204) {
                logger.warn("Failed to delete group from space: ${space.key}")
            } else {
                logger.info("Group removed from space: ${space.key}")
                removedSpaceKeys << space.key
            }
        }
    }
}
removedSpaceKeys

def getAllItems(String nextUrl, Class clazz) {
    def items = []
    def url = nextUrl
    while (url) {
        def response = get(url).asObject(clazz).body
        items.addAll(response["results"])
        url = response["_links"]["next"]
    }
    items
}
Having an issue with this script?
Report it here