Skip to content

cleanup blueprint orphans agent instances (identity SP + agentic user) #258

@pratapladhani

Description

@pratapladhani

Description

a365 cleanup blueprint deletes the blueprint Entra ID application and messaging endpoint, but does not check for or clean up any agent instances (agent identity service principal + agentic user) that were created from that blueprint.

This leaves orphaned resources in Entra ID that cannot be cleaned up by a365 cleanup instance (see related issue).

Steps to Reproduce

  1. a365 setup all — creates blueprint + deploys infrastructure
  2. a365 publish — publishes blueprint to MOS catalog
  3. Admin activates blueprint, user creates instance → agent identity SP + agentic user created
  4. a365 cleanup blueprint — deletes blueprint app, federated credentials, and messaging endpoint
  5. Agent identity SP and agentic user remain in Entra ID with no parent blueprint

Expected Behavior

cleanup blueprint should either:

  • Option A: Detect active instances and warn/refuse to delete until instances are cleaned up first
  • Option B: Cascade cleanup — delete instances (identity SP + agentic user) before deleting the blueprint
  • Option C: At minimum, warn the user that orphaned instances will remain

Actual Behavior

Blueprint is deleted silently. The agent identity SP (servicePrincipalType: ServiceIdentity) and agentic user remain in Entra ID. These orphaned resources:

  • Still appear in GET /beta/servicePrincipals/microsoft.graph.agentIdentity
  • Still appear in GET /beta/agentUsers
  • Cannot be cleaned up via a365 cleanup instance (separate bug — AgenticAppId/AgenticUserId are never populated)
  • Must be manually deleted via Graph API or Entra portal

Code Reference

CleanupCommand.cs CreateBlueprintCleanupCommand handler deletes:

  1. Federated credentials
  2. Blueprint application (via agentBlueprintService.DeleteAgentBlueprintAsync)
  3. Messaging endpoint

But does not query for or delete:

  • Agent identity SP (would need GET /beta/servicePrincipals filtered by blueprint parent)
  • Agentic user (linked via identityParentId to the identity SP)

Impact

Users who run cleanup blueprint are left with orphaned Entra ID resources they cannot easily discover or remove. This is especially problematic in demo tenants where multiple blueprint create/delete cycles occur.

Suggested Fix

Before deleting the blueprint, query for instances:

GET /beta/servicePrincipals/microsoft.graph.agentIdentity

Filter results where the parent matches the blueprint being deleted. For each instance, delete the agentic user first (via DELETE /beta/agentUsers/{id} or az ad user delete), then the identity SP.

Metadata

Metadata

Labels

P1Very high prioritybugSomething isn't workingescalatedIssue has breached SLA and been escalatedsecuritySecurity-related issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions