Script to remove Inactive users who have never logged in

kdickason April 26, 2022

My company copied users from an AD database to a Jira Internal database.  I now have literally thousands of "extra" user accounts for users who are Inactive and have never logged in to Jira.  I would like to delete these users from the Jira Internal database.  I have ScriptRunner, and simply need help with a script.  Anyone willing to give it a shot and help me out?

I did try a script I found to at least strip their group assignments away, and although I got no errors, that script didn't work.  All the Inactive users are all still assigned to various groups.  Here's the info on that:  https://community.atlassian.com/t5/Jira-questions/ScriptRunner-script-to-remove-inactive-users-from-all-groups/qaq-p/632587?utm_source=atlcomm&utm_medium=email&utm_campaign=immediate_general_reply&utm_content=topic#U2012433

Would really appreciate some guidance!

2 answers

1 vote
Andrea Pannitti
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.
April 27, 2022

Hi @kdickason,

you can do it with this script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager

def loginManager = ComponentAccessor.getComponent(LoginManager.class);
def adminUser = ComponentAccessor.userManager.getUserByName("admin") // Replace "admin" with an administrator username
def userUtil = ComponentAccessor.userUtil

ComponentAccessor.userManager.allUsers.each{ it->
if (loginManager.getLoginInfo(it.username).loginCount == null) {
log.error "User never logged in: " + it.username
//userUtil.removeUser(adminUser, it) // Uncomment this row to enable removal
}
}
kdickason April 27, 2022

This is wonderful for you to provide.  But please allow me to ask -- what would I need to add to this script so it would only remove INACTIVE users who have never logged in? I would like to keep Active accounts who have never logged in for now. @Andrea Pannitti 

Andrea Pannitti
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.
April 27, 2022

@kdickason you could change the code like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager

def loginManager = ComponentAccessor.getComponent(LoginManager.class);
def adminUser = ComponentAccessor.userManager.getUserByName("admin") // Replace "admin" with an administrator username
def userUtil = ComponentAccessor.userUtil

ComponentAccessor.userManager.allUsers.findAll{ !it.isActive() && loginManager.getLoginInfo(it.username).loginCount == null }.each{ it->
log.error "User inactive and never logged in: " + it.username
//userUtil.removeUser(adminUser, it) // Uncomment this row to enable removal
}

 

0 votes
Tushar Gohel April 26, 2022

Hi @kdickason 

 

I don't have a script but we are using Manage Inactive Users for Jira add-on to remove the never logged-in users from our system.

 

Thanks,

Tushar

Suggest an answer

Log in or Sign up to answer