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 (8.0 - 8.14)

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 groovy.xml.MarkupBuilder final 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) def 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) { return 'No Idle users found' } def stringWriter = new StringWriter() def content = new MarkupBuilder(stringWriter) content.html { p('Follow users deactivated ') { ul { idleUsers.each { user -> deactivateUser(user) }*.username?.each { deactivated -> li(deactivated) } } } } stringWriter.toString() 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