Azure Service Principals are not deleting from the Azure CLI

74 views Asked by At

I'm executing the code below, and it appears that the delete method isn't able to detect all the service principals like the list method does. Has anyone else encountered this problem?

enter image description here

1

There are 1 answers

4
Rukmini On

The az ad sp list by default lists only the first 100 Service Principals. Hence you need to use --all flag to list all the Service Principals.

Hence to resolve the error, you need to modify the script by using az ad sp list --all --query "[].appId" -o tsv like below:

sp_list=$(az ad sp list --all --query "[].appId" -o tsv)
for sp_id in $sp_list;do
    echo "Deleting service principal: $sp_id" 
    az ad sp delete --id $sp_id
done
echo "All Service Principals deleted" 

For sample, I tried to delete few Service Principals by using -all flag (as I cannot delete all Service Principals in my environment):

I tried to delete the below applications:

enter image description here

The service principals got deleted successfully:

sp_list=$(az ad sp list --all --query "[?displayName=='cli1' || displayName=='cli2' || displayName=='cli3' || displayName=='cli4' || displayName=='cli5'].appId" -o tsv)
for sp_id in $sp_list;do
    echo "Deleting service principal: $sp_id" 
    az ad sp delete --id $sp_id
done
echo "All Service Principals deleted" 

enter image description here