Script to remove a list of users from all groups

Peter Ellis January 28, 2019

Hi,

I have some regular user management clean up where I need to remove a list of users from all the groups that they are in. Jira server v7 and using ScriptRunner Script Console. Can anyone help?

Much appreciated

2 answers

1 accepted

0 votes
Answer accepted
Mario Carabelli
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.
January 29, 2019

Hi @Peter Ellis,

this script uses the Jira API 7.6.1 and worked on my system (Jira Server 7.9).


import com.atlassian.jira.component.ComponentAccessor

List<String> userNames = ["testuser"] // add all usernames in this array where you want to remove the groups

def userUtil = ComponentAccessor.getUserUtil()
def userManager = ComponentAccessor.getUserManager()
for (String userName in userNames) {
 def groups = userUtil.getGroupsForUser(userName)
 def user = userManager.getUserByName(userName)
 for (def group in groups) {
   userUtil.removeUserFromGroup(group, user)
 }
}

With kind regards,
Mario

Peter Ellis January 29, 2019

Mario,

That's brilliant, works a treat. Many thanks.

Peter 

Mario Carabelli
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.
January 29, 2019

Hi @Peter Ellis,

good  idea to create a script for this usecas!

I will use this myself :)

With kind regards
Mario

Peter Ellis February 5, 2019

Hi @Mario Carabelli,

Would you know how to deactivate the user too?

Best regards

Peter

0 votes
Heiko Gerlach
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!
February 16, 2022

Hi @[deleted]

nice script:-)

Due to performance reasons I modified it as shown... (changed removeUserFromGroup to removeUserFromGroups)

import com.atlassian.jira.component.ComponentAccessor

List<String> userNames = ["TestUser"] // add all usernames in this array where you want to remove the groups

def userUtil = ComponentAccessor.getUserUtil()
def userManager = ComponentAccessor.getUserManager()
for (String userName in userNames) {
def groups = userUtil.getGroupsForUser(userName)
def user = userManager.getUserByName(userName)
userUtil.removeUserFromGroups(groups, user)
}

 Cheers

Heiko

Salim Hammar
Contributor
October 10, 2022

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events