Bulk delete groups in JIRA using Scriptrunner

Sid June 3, 2019

Due to some syncing error, we have over 2000 groups created in JIRA (all of those groups are empty). I am trying to find a scriptrunner code to bulk delete all of them. Any help would be appreciated.

Thanks

1 answer

3 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 5, 2019

Something along these lines might work (untested)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.core.util.StaticCrowdServiceFactory

def crowdService = StaticCrowdServiceFactory.crowdService
def groupManager = ComponentAccessor.groupManager

groupManager.getAllGroups().findAll {
groupManager.getDirectUsersInGroup(it).size() == 0
}.each{
crowdService.removeGroup(it)
}
Sid June 6, 2019

Thank you @Peter-Dave Sheehan. The script worked perfectly. 

Is there a way to specify a particular set of groups say group1, group2, group3 and delete those?

Thanks,

bney

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 6, 2019

Of course ...

Btw, if you haven't been looking there already ... you will want to start:

https://docs.atlassian.com/software/jira/docs/api/7.13.2/com/atlassian/jira/component/ComponentAccessor.html

And while I was there, I noticed that "getAllGroups" is deprecated and may not work in the future.

The following will work:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.group.search.GroupPickerSearchService

import com.atlassian.crowd.embedded.core.util.StaticCrowdServiceFactory

def crowdService = StaticCrowdServiceFactory.crowdService
def groupSearch = ComponentAccessor.getComponent(GroupPickerSearchService.class);
def groupManager = ComponentAccessor.groupManager
def myGroupList = ['group1','group2','group3']
groupSearch .findGroups("").findAll {
groupManager.getDirectUsersInGroup(it).size() == 0 && it.name in myGroupList
}.each{
crowdService.removeGroup(it)
}
Like # people like this
Sid June 8, 2019

Worked like a charm. 

Thank you @Peter-Dave Sheehan 

Ramanathan Meyyappan February 27, 2024

@Peter-Dave Sheehan - quick question... if getallgroups is deprecated, what else would work? I have a usecase. I need to determine which are the groups which are associated with at least one configuration object within jira or confluence. All these groups are synced from AD in our case. So - my plan is to disable the sync, delete all groups - I would assume the delete would not work for groups which are tied with at least one object and I would hope it would capture the error and move forward ti the next group. We can decide that all groups that remain undeleted are the ones that are associated with at least an object and hence are required. The plan is to then add an attribute to those groups and change the filter and sync only those groups enabling the sync again. Let me know if this would work and what sort of a code I'm looking at for this? Any means to capture and refer the output/log of the execution?

 

Thanks and Regards,

Ram

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 27, 2024

I'm not sure. I haven't messed with that sort of scenario.

In terms of non-deprecated alternatives for getting all groups in your instance there is now a "getAllGroupNames" method in the GroupManager class.

Or you can use the GroupPickerSearchService

import com.atlassian.jira.bc.group.search.GroupPickerSearchService
import com.atlassian.jira.component.ComponentAccessor

def allGroups = ComponentAccessor.getComponent(GroupPickerSearchService).findGroups('')

Suggest an answer

Log in or Sign up to answer