Delete lots of old LDAP users


I import my users from an Active Directory. The AD user only can read (not write). Now I want to delete invalid users (580), which didn’t exist in the AD an more.

I know the command “sudo -u www-data php occ user:delete invalid_user”, but I don’t want to do I 580 times by hand. Is the any way to do it automatic?


Nextcloud version: 11
Operating system and version: Ubuntu 16.04
Apache: 2.4.18
PHP version: 7

With the help of a short script you can probably pass the output of occ ldap:show-remnants to occ user:delete username. I don’t know this by heart and would need to search and test myself.

It works in the following way:

sudo -u www-data php occ ldap:show-remnants > user.txt

Open user.txt with libreoffice or something else, extract the column with the usernames an save it to user.txt.

Save this bash script under

while read -r line
php occ user:delete "$name"
done < “$filename”

Execute the following command:

sudo -u www-data bash user.txt


I think it’s even simpler in a single line:

sudo -u www-data php occ ldap:show-remnants | awk 'FNR > 3 {print $2}' | sed '$d' | xargs -L1 sudo -u www-data php occ user:delete

This line worked for me perfect! Thank you!