{"record":{"id":"bcaf58f5e11c0487","repo":"microsoft/aspire","slug":"the-azure-deployment-state-for-aks-environment-name-does-not","errorCode":null,"errorMessage":"The Azure deployment state for AKS environment '{Name}' does not contain the deployed cluster identity.","messagePattern":"The Azure deployment state for AKS environment '(.+?)' does not contain the deployed cluster identity\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentResource.AksPipeline.cs","lineNumber":399,"sourceCode":"        //     \"name\": { \"type\": \"String\", \"value\": \"aks-abc123\" }\n        //   }\n        // Read it directly because the provisioning step that normally populates Outputs is not\n        // part of a fresh destroy process.\n        ResourceIdentifier? clusterResourceId;\n        try\n        {\n            clusterResourceId = GetPersistedAksResourceId(deploymentStateSection.Data);\n        }\n        catch (Exception ex) when (ex is not OperationCanceledException)\n        {\n            throw new InvalidOperationException(\n                $\"The Azure deployment state for AKS environment '{Name}' contains invalid outputs.\",\n                ex);\n        }\n\n        if (clusterResourceId is null)\n        {\n            throw new InvalidOperationException(\n                $\"The Azure deployment state for AKS environment '{Name}' does not contain the deployed cluster identity.\");\n        }\n\n        // Scope is persisted as a JSON string using the same shape produced by\n        // BicepUtilities.SetScopeAsync:\n        //   { \"resourceGroup\": \"shared-rg\", \"subscription\": \"00000000-...\" }\n        // A missing property means the resource did not pin that scope value, so only that value\n        // falls back to global Azure deployment state. Older state without Scope uses the persisted\n        // resource ID so a changed AppHost scope cannot redirect cleanup to another cluster or wait\n        // on provisioning that is not part of the destroy graph.\n        string subscriptionId;\n        string? resourceGroupName;\n        if (deploymentStateSection.Data[\"Scope\"] is not null)\n        {\n            string? scopedSubscription;\n            string? scopedResourceGroup;\n            try\n            {","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentResource.AksPipeline.cs#L381-L417","documentation":"Thrown by GetAksCredentialsForDestroyAsync when the cached Azure deployment state for the AKS environment has no 'ClusterResourceId' output. The library reads the deployment outputs left by a prior deploy to find the managed cluster ID during destroy; a missing output means the state is incomplete or from a different/failed deployment.","triggerScenarios":"Running aspire destroy/publish pipeline (getDestroyCredentialsStep) for an AzureKubernetesEnvironmentResource whose recorded deployment state lacks the cluster resource ID output.","commonSituations":"The Bicep module was edited and no longer emits the cluster ID output; the deployment state file was truncated, hand-edited, or written by a partially failed deploy; deploying a newer/older template than the one that created the state.","solutions":["Re-deploy the AKS environment so fresh, complete deployment state (including the cluster identity output) is written, then retry destroy","Verify the Bicep template still emits the cluster resource ID output and was not customized/trimmed","Delete the stale deployment state for this environment so the pipeline treats it as never deployed and re-runs the full flow","If the cluster was deleted out-of-band, remove the environment from the app model or its state entry instead of destroying"],"exampleFix":"// before: destroying with stale state after manually trimming Bicep outputs\n// output clusterResourceId string\n// after: restore the output in the AKS Bicep module\n// output clusterResourceId string = aksCluster.id\n// then: aspire run/publish to refresh state before destroy","handlingStrategy":"validation","validationCode":"var state = await GetDeploymentStateAsync();\nif (state.Data is null || !state.Data.ContainsKey(\"ClusterResourceId\"))\n    throw new InvalidOperationException(\"Deployment state missing ClusterResourceId; redeploy before destroy.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never hand-edit or trim the AKS Bicep outputs","Keep deploy state files intact between deploy and destroy","Redeploy after any template change before destroying"],"tags":["azure","aks","deployment-state","destroy"],"backgroundTag":"record-not-found","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}