It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Groovy Script to remove user from all component lead roles


I have written a groovy script to deactivate users.  The issue I am running into is that the script will fail if the user is a component lead.  What I want to do is check if the user is a component lead and remove them if they are.  So far I have been struggling to do so.  Any help would be much appreciated.


Script to deactivate user:


def deActivateUser(user, email, fullName, userService, userUtil){

deActivate = ImmutableUser.newUser(user).active(false).emailAddress("DELETE.${email}").displayName("${fullName} - OLD").toUser()
//user project or component lead?
 def userProjectLead = userUtil.getProjectsLeadBy(user)
 def userComponentLead = ComponentAccessor.projectComponentManager.findComponentsByLead("userName")
//get the request
 updateUserValidationResult = userService.validateUpdateUser(deActivate)

if (!userProjectLead && !userComponentLead) {
userUtil.removeUserFromGroup(group, user)
return "User ${user} deactivated"

2 answers

Hi Justin,

As you said one of the reasons that the script could fail to deactivate the user is because he/she is a project or component lead. So, in this case you have to remove him/her from the lead roles first. This worked for me, hope to work for you as well

 * Groovy Script to remove user from all component lead roles
 * and deactivate him.
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.bc.project.component.MutableProjectComponent
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.DelegatingApplicationUser

def userService = ComponentAccessor.getComponent(UserService.class)
def userName = "bob"

// get the project components where user leads
def componentsLeadedBy = ComponentAccessor.projectComponentManager.findComponentsByLead(userName)
def userManager = ComponentAccessor.getUserManager();

// Get the projects where user leads
def applicationUser = userManager.getUserByName(userName);
def projectLeadedBy = ComponentAccessor.userUtil.getProjectsLeadBy(applicationUser);

def projectManager = ComponentAccessor.getProjectManager();
def projectComponentManager = ComponentAccessor.getProjectComponentManager();
if (!componentsLeadedBy && !projectLeadedBy) "userName doesn't lead any component or project"
else {
    //remove userName from project lead roles
    if (projectLeadedBy.size() > 0)
        for (project in projectLeadedBy) {
            projectManager.updateProject(project,, project.description, "", project.url, project.assigneeType);

    //remove userName from component lead roles
    if (componentsLeadedBy.size() > 0)
        for (component in componentsLeadedBy) {
            MutableProjectComponent newComponent = MutableProjectComponent.copy(component)

def listUsers = userManager.getUsers().findAll{user ->}
if (listUsers.size() <= 0)
    return "User userName does not exist"
ImmutableUser.Builder builder = ImmutableUser.newUser(listUsers[0]);;
def updatedUser = new DelegatingApplicationUser(applicationUser.key, builder.toUser())

UserService.UpdateUserValidationResult updateUserValidationResult =
if (updateUserValidationResult.isValid()) {
    return "User ${} deactivated";
} else {
    return updateUserValidationResult.getErrorCollection()

Hi Justin,

Thanks for script, but i am getting below error message while executing through script console in jira 7.0.11

kindly help in fixing this issue.

If possible please provide any URL for learning Groovy with intention of Jira since i have basic nowledge of Java.




error message

2017-04-21 08:13:29,996 WARN [common.UserScriptEndpoint]: Script console script failed: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script269.groovy: 24: expecting ')', found ';' @ line 24, column 29. if (!componentsLeadedBy && !projectLeadedBy) ^ 1 error

Yes the Atlassian Community is fairly new and has some issues to fix, one of them is how the code is rendered. So in your case

line 24: if (!componentsLeadedBy && !projectLeadedBy)

line 28:  if (projectLeadedBy.size() > 0)

line 34:  if (componentsLeadedBy.size() > 0)

line 42: def listUsers = userManager.getUsers().findAll{user ->}


Hi Thanos,

Apprciate your quick help and now i am getting below error

2017-04-21 09:33:32,033 WARN [common.UserScriptEndpoint]: Script console script failed: java.lang.NullPointerException at com.atlassian.crowd.embedded.impl.ImmutableUser.newUser( at com.atlassian.crowd.embedded.impl.ImmutableUser$ Source) at

line numbers are 45,47.

Please suggest any editor or something for fixing rendering related issue in code.

I want to learn this Groovy but understanding complete  syntex itself is confusing so kindly provide any tutorial link to learn from scratch to advance



I solved myself after learning some concepts in Groovy



@Balvant Biradar  Could you let us know what is the fix,.

Suggest an answer

Log in or Sign up to answer

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you