Azure Service Principals are not deleting from the Azure CLI

Petteys, Kevin 91 Reputation points
2024-03-27T19:18:02.3466667+00:00

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?

User's image

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,474 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Akhilesh 4,775 Reputation points Microsoft Vendor
    2024-03-28T09:51:28.9433333+00:00

    Hi @Petteys, Kevin

    Thanks for posting your question in the Microsoft Q&A forum!

    For your query I understand that you are trying to delete the Mutiple Azure service principle, and you are facing an issue with an error.

    In your Azure CLI script, you are trying to delete the Mutiple service principals to do that you are using az ad sp list which allow you delete first 100 service principals.
    To delete required multiple service principals, you need to list all Service Principals with display names in your script which has shown in the below.

    sp_list=$(az ad sp list --all --query "[?displayName=='app1' || displayName=='app2' || displayName=='app3' || displayName=='app4'].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" 
    

    Replace app1, app2, app3 and app4 with the application name.

    If you would like to delete the individual service principals, you can use the below command.

    az ad sp delete --id myServicePrincipalID
    

    Replace myServicePrincipalID with the object ID of your service principle.

    Hope this helps. Do let us know if you any further queries.

    Reference: https://learn.microsoft.com/en-us/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-delete

    Thanks,

    Akhilesh.

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.