Home / Azure

AzureRunbookAdvanced

Sev1: Entra ID app registration or service principal deleted. Sign-ins and pipelines failing with AADSTS700016

A cleanup script or admin deleted an app registration or enterprise app and logins, APIs and deployments broke. Restore both objects from soft delete within 30 days and check role assignments and consent.

Deepak Bhardwaj5 min read

Applies toMicrosoft Entra ID app registrations and service principals (enterprise applications), Microsoft Graph v1.0, Azure CLI 2.x, Microsoft Graph PowerShell SDK; managed identities are not restorable this way

Symptoms

  • Users get AADSTS700016: Application with identifier '<appId>' was not found in the directory on sign-in to an internal app.
  • Daemons, CI/CD pipelines and service connections fail to get tokens with the same error, or with invalid_client.
  • Azure role assignments for the app show Identity not found in Access control (IAM).
  • az ad sp show --id <appId> returns "Resource not found", although the app worked an hour ago.

Deleted app registrations and service principals stay in a soft-deleted state for 30 days. Restoring them brings back the same object IDs, client ID, credentials and configuration. Creating a new app does not; it gets a new client ID and every client has to be reconfigured. Restore first.

Caution

Do not "fix" this by creating a new app registration with the same name, and do not permanently delete anything from Deleted applications. A permanently deleted object cannot be restored, and a new app breaks every client that has the old client ID.

1. Confirm scope and impact (5 minutes)

Get the client ID (appId) from the error message or the failing app's configuration, then check what still exists:

APPID=00000000-0000-0000-0000-000000000000

az ad app show --id $APPID --query "{name:displayName, objectId:id}" -o table   # app registration
az ad sp show  --id $APPID --query "{name:displayName, objectId:id}" -o table   # enterprise app / service principal
App registration Service principal Meaning
Missing Missing App deleted (the service principal goes with it)
Exists Missing Only the enterprise application was deleted
Missing Exists App is multi-tenant from another tenant, or the home tenant deleted it

2. Find who deleted it and when

az rest --method get \
  --url "https://graph.microsoft.com/v1.0/auditLogs/directoryAudits?\$filter=activityDisplayName eq 'Delete application'&\$top=20" \
  --query "value[].{time:activityDateTime, activity:activityDisplayName, by:initiatedBy.user.userPrincipalName, app:initiatedBy.app.displayName, target:targetResources[0].displayName}" -o table

Run it again with 'Delete service principal' if only the enterprise application is missing. If an automation account or script did the delete (initiatedBy.app), stop it now so it doesn't delete the restored object again or move on to other apps. Disable the schedule or revoke the identity's Graph permission.

3. Restore the app registration

List soft-deleted applications and find the object ID. This is the deleted object's id, not the appId:

az rest --method get \
  --url "https://graph.microsoft.com/v1.0/directory/deletedItems/microsoft.graph.application" \
  --query "value[?appId=='$APPID'].{objectId:id, name:displayName, deleted:deletedDateTime}" -o table

Restore it:

OBJID=<objectId from above>
az rest --method post \
  --url "https://graph.microsoft.com/v1.0/directory/deletedItems/$OBJID/restore"

The same in Microsoft Graph PowerShell:

Connect-MgGraph -Scopes "Application.ReadWrite.All"
Get-MgDirectoryDeletedItemAsApplication -All | Where-Object AppId -eq $AppId |
  Select-Object Id, DisplayName, DeletedDateTime
Restore-MgDirectoryDeletedItem -DirectoryObjectId $DeletedObjectId

You need a role that can manage applications, such as Application Administrator or Cloud Application Administrator, or Application.ReadWrite.All.

4. Restore the service principal separately

Restoring the app registration does not bring back its service principal. Restore that as a second step, using the deleted service principal's object ID:

az rest --method get \
  --url "https://graph.microsoft.com/v1.0/directory/deletedItems/microsoft.graph.servicePrincipal" \
  --query "value[?appId=='$APPID'].{objectId:id, name:displayName, deleted:deletedDateTime}" -o table

az rest --method post \
  --url "https://graph.microsoft.com/v1.0/directory/deletedItems/<sp-objectId>/restore"

Only if the service principal is not in deleted items (past 30 days, or permanently deleted) create a new one for the restored app:

az ad sp create --id $APPID

Warning

A new service principal has a new object ID. Azure role assignments, Key Vault access policies, SQL contained users (CREATE USER ... FROM EXTERNAL PROVIDER), group memberships, app role assignments and admin consent all point at the old object ID and must be re-created.

5. Check permissions and assignments

SPID=$(az ad sp show --id $APPID --query id -o tsv)

# Azure RBAC
az role assignment list --assignee $SPID --all -o table

# Orphaned assignments ("Identity not found") usually show an empty principalName
az role assignment list --all --query "[?principalName==''].{role:roleDefinitionName, scope:scope, principalId:principalId}" -o table

With a restored service principal (same object ID), existing role assignments that were not cleaned up start working again. If a cleanup removed them, re-create them from IaC or from the orphaned list above. Then check, in the enterprise application:

  • Permissions: admin consent for API permissions is present. Grant it again if it is missing.
  • Users and groups: assignments exist if "Assignment required" is on.
  • Single sign-on and Provisioning settings for SAML or SCIM apps.

6. Verify recovery

  • az ad sp show --id $APPID and az ad app show --id $APPID both return the original object IDs.
  • A test client gets a token:
az login --service-principal -u $APPID -p "$SECRET" --tenant <tenant-id> --allow-no-subscriptions
az account get-access-token --resource https://management.azure.com/ --query expiresOn -o tsv
  • Interactive sign-in to the app works for a test user. Sign-in logs show success for the app.
  • Pipelines and service connections run green.

Communicate

"Sign-in to and automated jobs using it failed from

Prevent it next time

  • Alert on Delete application and Delete service principal audit events by sending Entra audit logs to Log Analytics:
AuditLogs
| where OperationName in ("Delete application", "Delete service principal")
| project TimeGenerated, OperationName, InitiatedBy, TargetResources
  • Make cleanup scripts soft-delete only, log what they delete, and exclude apps tagged as production or owned by a platform team.
  • Give every production app at least two owners and keep its role assignments and consent in IaC.
  • Make Application Administrator and Cloud Application Administrator eligible-only roles in PIM, with approval on activation, so nobody holds delete rights all the time.
  • Record which apps each critical service depends on (client IDs) in the runbook for that service.