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.

1 answer

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

Hi Thanos,

it worked like a charm for me!

Thank you!

Alberto

Suggest an answer

Log in or Sign up to answer