How to delete users in jira with API

Petik Vardanyan May 16, 2024

There are several users that must be deleted. I must create a script to delete all of them. 

I search for the documentation's solutions but they don't work.


curl --request DELETE \ --url 'https://api.atlassian.com/admin/v1/orgs/{orgId}/directory/users/{accountId}' \ --header 'Authorization: Bearer <access_token>'


as well generated the script

import csv
import requests


url_template = "https://api.atlassian.com/admin/v1/orgs/{orgId}/directory/users/d{account_id}"

def delete_user(url, access_token):
headers = {"Authorization": f"Bearer {access_token}"}
response = requests.delete(url, headers=headers)
if response.status_code == 204:
print(f"User with account ID {account_id} deleted successfully.")
else:
print(f"Failed to delete user with account ID {account_id}. Status code: {response.status_code}")

access_token = input("Enter your access token: ")
org_id = input("Enter your organization ID: ")
csv_file = input("Enter the path to the CSV file: ")

with open(csv_file, "r") as file:
reader = csv.reader(file)
for row in reader:
account_id = row[0]
url = url_template.format(orgId=org_id, account_id=account_id)
delete_user(url,access_token)

1 answer

0 votes
Abhishek Jain
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 19, 2024

Hi Petik,

If the user accounts are all managed accounts then you should be able to delete these users in bulk using the below user management API 

https://developer.atlassian.com/cloud/admin/user-management/rest/api-group-lifecycle/#api-users-account-id-manage-lifecycle-delete-post 

Petik Vardanyan May 22, 2024

Hi again, thank you for the answer
I've checked that POST request.

curl --request POST \ --url 'https://api.atlassian.com/users/{account_id}/manage/lifecycle/delete' \ --header 'Authorization: Bearer <access_token>' \ --header 'Accept: application/json'

i did filled the account_id, it's okay
the part with the access_token. I entered 2 different tokens:

1 is from atlassian account > security > Create and manage API tokens

2 is from the place where it gives me the organization id and the token at the same time

which of them should have been valid, bcs after the using each of them it says that i am not authorized.

so the question is , which token/key must be used? And during the call is it important to use the call with the device where the creator account is logged ?

Abhishek Jain
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 22, 2024

Hi Petik,

You need to use the Admin API keys to execute this API. 

You can create one by following the steps in the below mentioned document:

https://support.atlassian.com/organization-administration/docs/manage-an-organization-with-the-admin-apis/

Petik Vardanyan May 22, 2024

Yes,  I do have those values , but when use that POST request, it says that i am not authorized..

after that POST request, it says
{"code":401,"message":"Unauthorized"}

Abhishek Jain
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 23, 2024

Petik, 

If you are using the right Admin API keys, you should not have any issues when trying to delete a user. 

The only use case I can think of is when the user is provisioned from the IDP; in this case, you will not be allowed to delete the user.

If you can share any other details then it will be helpful. 

 

Petik Vardanyan May 23, 2024

Will it make a problem, if I don't have atlassian admin. I have only api key?

Petik Vardanyan May 24, 2024

there are users that are local and made with IDP , but none of them are deleted. I just don't know why it gives 401 error

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events