Deactivate Idle Users


Running this script from the Script Console deactivates users that have never logged in.


I want to deactivate all users in my Jira instance who have never logged in to keep my instance tidy and our license costs under control. Running the following script from the Script Console will deactivate users that have never logged in, keeping your instance tidy and your license costs under control.


Jira Jira (7.7 - 8.6)

import import com.atlassian.jira.bc.JiraServiceContextImpl import com.atlassian.jira.bc.user.UserService import import import com.atlassian.jira.component.ComponentAccessor import import com.atlassian.jira.user.ApplicationUser import static com.onresolve.scriptrunner.canned.util.OutputFormatter.markupBuilder final String directoryToCheck = "JIRA Internal Directory" def loginManager = ComponentAccessor.getComponent(LoginManager) def directoryManager = ComponentAccessor.getComponent(DirectoryManager) UserSearchParams.Builder paramBuilder = UserSearchParams.builder() .allowEmptyQuery(true) .includeActive(true) .includeInactive(false) JiraServiceContextImpl jiraServiceContext = new JiraServiceContextImpl(ComponentAccessor.jiraAuthenticationContext.loggedInUser) def allActiveUsers = ComponentAccessor.getComponent(UserSearchService).findUsers(jiraServiceContext, "", def internalDirectoryId = directoryManager.findAllDirectories()?.find { == directoryToCheck.toLowerCase() }?.id def idleUsers = allActiveUsers.findAll { user -> user.directoryId == internalDirectoryId && !loginManager.getLoginInfo(user.username)?.lastLoginTime } if (idleUsers) { markupBuilder { p('Follow users deactivated ') { ul { idleUsers.each { user -> deactivateUser(user) }*.username?.each { deactivated -> li(deactivated) } } } } } else { "No Idle users found" } def deactivateUser(ApplicationUser user) { def userService = ComponentAccessor.getComponent(UserService) def updateUser = userService.newUserBuilder(user).active(false).build() def updateUserValidationResult = userService.validateUpdateUser(updateUser) if (!updateUserValidationResult.valid) { log.error "Update of ${} failed. ${updateUserValidationResult.errorCollection}" return } userService.updateUser(updateUserValidationResult) "${} deactivated" }
Discovered an issue? Report it here

Suggested for you

Appears in these Collections