Bulk delete users

Mary Mark February 9, 2018

I'm trying to find a way to delete users from JIRA software from the user directory: JIRA Internal Directory as they are already existing in the LDAP directories. There are about 1000 users to remove.

I am wondering if you can provide some guidance on using script console to delete the users.

The code I have is the following but does not capture the user directory piece.


import com.atlassian.jira.component.ComponentAccessor


def userUtil = ComponentAccessor.userUtil
def userManager = ComponentAccessor.userManager

def usersToRemove = ["exampleUser1","exampleUser2","exampleUser3","exampleUser4","exampleUser5"] //User Names


usersToRemove.each{
def currentUser = userManager.getUserByName(it)

}

 
The rest api: http://<JIRA URL>/jira/rest/api/2/user/
However I am getting a page cannot display error when launched. 


 Not familiar with scripting/coding.

2 answers

3 votes
Thanos Batagiannis [Adaptavist]
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.
February 12, 2018

Hi Mary,

From your Script Console you can try a script like 

import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor

def userService = ComponentAccessor.getComponent(UserService)
def asUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

//usernames to remove
def userNames = ["Paolo", "Niki", "Christina", "Omar", "Peter", "Borge", "Anna", "Lena"]

userNames?.each { it ->
final UserService.DeleteUserValidationResult result = userService.validateDeleteUser(asUser, it)

if (result.isValid()) {
userService.removeUser(asUser, result)
log.info "User with username $it removed"
}
else {
log.error "Failed to remove user with username $it. " + result.getErrorCollection().errorMessages
}
}

 Let me know how this went. 

Regards, Thanos

Alberto Carrani
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!
September 20, 2021

Hi Thanos,

it worked like a charm for me!

Thank you!

Alberto

Angie Affolter
Contributor
October 29, 2024

I know you posted this a few years back so maybe that's my issue but it's ignoring the first name and errors out the second and third:

2024-10-29T14:23:27,932 INFO [events.EventReplicationManager]: [REPLICATED-EVENT] Event:SessionInvalidationReplicatedEvent:com.atlassian.jira.web.session.cluster.SessionInvalidationReplicatedEvent@97f65b0 will be replicated to cluster. 2024-10-29T14:23:27,932 INFO [events.EventReplicationManager]: [REPLICATED-EVENT] cache.onRemove 2024-10-29T14:23:27,960 ERROR [runner.ScriptBindingsManager]: Failed to remove user with username ryan.paternoste. [This user does not exist please select a user from the user browser.] 2024-10-29T14:23:27,960 ERROR [runner.ScriptBindingsManager]: Failed to remove user with username ron.verna. [This user does not exist please select a user from the user browser.]

Note my users are inactive in the Internal Jira directory and don't have a jira software license assigned. 

I assume that in the usernames I should enter usernames which I did.  Here is my script:

import com.atlassian.jira.bc.user.UserService

import com.atlassian.jira.component.ComponentAccessor

 

def userService = ComponentAccessor.getComponent(UserService)

def asUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

 

//usernames to remove

def userNames = [“paul.mcnally”, “ryan.paternoster”, “ron.verna”]

 

userNames?.each { it ->

    final UserService.DeleteUserValidationResult result = userService.validateDeleteUser(asUser, it)

 

    if (result.isValid()) {

        userService.removeUser(asUser, result)

        log.info "User with username $it removed"

    }

    else {

        log.error "Failed to remove user with username $it. " + result.getErrorCollection().errorMessages

    }

}

 

Any help would be appreciated!  Thanks, Angie

Hugo Navia
Contributor
November 7, 2024

Hi Angie,

The script is outdated as it relies on the UserService API, which may not align with newer versions of Jira (8.x or higher). Recent Jira updates use ApplicationUser objects and deprecate certain direct user management methods. Here's an updated version of the script that should work with the current Jira API structure. I've done some modifications but I was not able to test them yet. Please let me know if it works for you:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def userManager = ComponentAccessor.getUserManager()
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

// List of usernames to remove
def userNames = ["Paolo", "Niki", "Christina", "Omar", "Peter", "Borge", "Anna", "Lena"]

userNames?.each { username ->
ApplicationUser userToDelete = userManager.getUserByName(username)

if (userToDelete) {
try {
userManager.removeUser(loggedInUser, userToDelete)
log.info "User with username '$username' removed successfully."
} catch (Exception e) {
log.error "Failed to remove user with username '$username': ${e.message}", e
}
} else {
log.warn "User with username '$username' does not exist."
}
}
0 votes
Angie Affolter
Contributor
November 7, 2024

I can't remember what my issue is but I did get this script to work:

import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor

def userService = ComponentAccessor.getComponent(UserService)
def asUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

//usernames to remove
def userNames = ["angie.affolter", "john.doe", "mary.smith"]

userNames?.each { it ->
    final UserService.DeleteUserValidationResult result = userService.validateDeleteUser(asUser, it)

    if (result.isValid()) {
        userService.removeUser(asUser, result)
        log.info "User with username $it removed"
    }
    else {
        log.error "Failed to remove user with username $it. " + result.getErrorCollection().errorMessages
    }
}

Suggest an answer

Log in or Sign up to answer