Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Deleting JSM customer users via API

delete.jpeg

Adding users into you instance is one thing, however removing these users could be another challenge. If you're familiar with Jira's REST API it should be your saving grace to tweak and automate certain things. However making these calls could be challenging for people who are new to it and sometimes you can't get it to work the way you want due to various reasons. Below I'm going to show you how to delete users from your Jira cloud instance (JSM customer users or Jira users) using API and in less than 20 lines of code. Sound exciting, how about you continue reading below to get the insight.

To do this, we're focusing on python and using a python package called jiraone (get it via pip install jiraone).

Deleting a user

In order to delete a user on cloud, you will need to know the accountId. How about if there was a way to delete a user without knowing the accountId and simply by just knowing the display name of the user? Well this feature already exist within Jira's API, you just need to know how to call it. Let's start off with a basic call using the jiraone package to delete a user.

from jiraone import LOGIN, endpoint, USER, echo

user = "email"
password = "token"
link = "https://example.atlassian.net"

LOGIN(user=user, password=password, url=link)
# search for users
name_of_user = "Elf App 607"
search = USER.search_user(name_of_user, pull="active", user_type="customer")
for name in search:
# delete a user that's found
delete = LOGIN.delete(endpoint.jira_user(name.get("accountId")))
# returns 204 for successful deletion
if delete.status_code < 300:
echo("User deleted...")
else:
echo(delete.status_code)
echo("User not deleted...")

Let me explain what is happening on the above code. We need to delete a user with a name of "Elf App 607". Now we're calling our search_user method and our arguments needs to pull only active users of type "customer" - This way we want to only search for JSM customer with that name. If the user exist and is found, we delete and get a response of 204. This same principle applies to Jira users in such a scenario change the user_type argument to "atlassian".

Bulk deleting users

I think we got the hang of things, how about we take this further into bulk deleting multiple users. Remember we're keeping our code within 20 lines and not looking for any accountId but by means of the user's display name. In order to do this, we'll need a file source to wrap our user's display names. You can do this by using a csv file and placing each user's name on a single line and save as a .csv file format. Example below

Elf App 607

Elf App 61

Elf App 405

from jiraone import LOGIN, endpoint, USER, echo, file_reader

args = "email", "token", "https://example.atlassian.net"
LOGIN(*args)
read = file_reader(file_name="user_files.csv")
get_user_list = []
get_account_id = []
for users in read:
get_user_list.append(users[0])
for names in get_user_list:
search = USER.search_user(names, pull="active", user_type="customer", skip=True)
for name in search:
get_account_id.append(name.get("accountId"))
for aid in get_account_id:
delete = LOGIN.delete(endpoint.jira_user(aid))
if delete.status_code < 300:
print("User deleted")
else:
echo("User not deleted...")

Now we're going for a bulk delete of users, we start by using a tuple that way we can reduce our code line to just one and keep it within 20 lines of code. It has to be in a sequence to follow user, password and url keyword arguments from the LOGIN variable as shown in the first example above. We prepare two empty list to capture the names of the users after being read and the accountId after being found. In the latter situations, if the users is not found, the script will automatically terminate with an error of user not found. Once we get all the user's accountId we've listed out, then we run a delete of the users.

2 comments

I'm getting a 404 status_code on trying to delete a user. Am I missing something?

404 generally means the resource was not found. Most probably, the user you're searching for was not found and returned 0 instead of a list containing key-value pairs of the user. Please can you print the result of the user search to see what's being returned?

Like Etienne likes this

Comment

Log in or Sign up to comment
TAGS
Community showcase
Published in Jira Service Management

Next level incident management in Jira Service Management

Hello Atlassian Community! We’re thrilled to announce some exciting changes to incident management in Jira Service Management Cloud, in order to give your teams extra muscle during incidents, and ...

1,699 views 21 12
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you