DELETING MULTIPLE USERS VIA WEB API - DHIS v2.29

Is there a way to bulk delete users on DHIS2 via web API? I am trying to avoid using SQL queries for this.

I attempted the following and curl query and got the response shown below:
curl -X DELETE -u username:password https://localhost:8080/dhis/api/26/userCredentials?fields=username,code&filter=username:^like:cf
[1] 24429

RESPONSE:

{“httpStatus”:“Method Not Allowed”,“httpStatusCode”:405,“status”:“ERROR”,“message”:“Request method ‘DELETE’ not supported”}

3 Likes

Hi @Nguza_Yikona; there is a related post to your approach of deleting in bulk via API; kindly check out this:- API: Mass-deleting data values and Bulk Deletion of Data - #2 by Lorill_Crees for guidance and since @Lars was handling this then, I’m sure he will update us on the developments to this approach too.

Hope this helps.

Best,
James.

4 Likes

Thanks for this @jomutsani . This is helpful. So it looks like the only way at the moment is using SQL.

Best,
Nguza

3 Likes

hi @Nguza_Yikona :smiley: !
3 months late, but I had to do this recently and thought Id share, you can write a loop in R or Python for deleting users in bulk.

in R…
base.url<-“XYZ.dhis. org/dhis”
source.username<-“ABC”
source.password<-“123”

#Create your filter
url<-paste0(base.url, “users.csv?fields=id&filter=name:ilike:john&paging=false”)

#pull data on users to delete
require(httr)
require(assertthat)
r<-GET(url,authenticate(source.username,source.password))
data<-httr::content(GET(url),“text”)
deletions<-read.csv(textConnection(data),header=TRUE)

#loop through all deletions. WARNING! CAREFUL WHAT YOU WISH FOR!
for (i in 1:length(deletions$id)){
id<-deletions$id[i]
delete_url<-paste0(url,id)
r<-DELETE(delete_url,authenticate(source.username,source.password))
print(id)
assert_that(r$status_code == 200L)
}

3 Likes

Hi @brian :smiley:

This is perfect!! works like a charm.

Thanks Brian!

3 Likes