{"record":{"id":"856f1ff85fd91e76","repo":"microsoft/semantic-kernel","slug":"the-thread-could-not-be-deleted-due-to-an-error-re","errorCode":null,"errorMessage":"The thread could not be deleted due to an error response from the service.","messagePattern":"The thread could not be deleted due to an error response from the service\\.","errorType":"exception","errorClass":"AgentThreadOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/AzureAI/AzureAIAgentThread.cs","lineNumber":113,"sourceCode":"        }\n    }\n\n    /// <inheritdoc />\n    protected override async Task DeleteInternalAsync(CancellationToken cancellationToken)\n    {\n        const string ErrorMessage = \"The thread could not be deleted due to an error response from the service.\";\n\n        try\n        {\n            await this._client.Threads.DeleteThreadAsync(this.Id, cancellationToken).ConfigureAwait(false);\n        }\n        catch (RequestFailedException ex) when (ex.Status == 404)\n        {\n            // Do nothing, since the thread was already deleted.\n        }\n        catch (RequestFailedException ex)\n        {\n            throw new AgentThreadOperationException(ErrorMessage, ex);\n        }\n        catch (AggregateException ex)\n        {\n            throw new AgentThreadOperationException(ErrorMessage, ex);\n        }\n    }\n\n    /// <inheritdoc />\n    protected override async Task OnNewMessageInternalAsync(ChatMessageContent newMessage, CancellationToken cancellationToken = default)\n    {\n        const string ErrorMessage = \"The message could not be added to the thread due to an error response from the service.\";\n\n        // If the message was generated by this agent, it is already in the thread and we shouldn't add it again.\n        if (newMessage.Metadata == null || !newMessage.Metadata.TryGetValue(\"ThreadId\", out var messageThreadId) || !string.Equals(messageThreadId, this.Id))\n        {\n            try\n            {\n                await AgentThreadActions.CreateMessageAsync(this._client, this.Id!, newMessage, cancellationToken).ConfigureAwait(false);","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/AzureAI/AzureAIAgentThread.cs#L95-L131","documentation":"Thrown by AzureAIAgentThread.DeleteInternalAsync when DeleteThreadAsync fails with a RequestFailedException whose status is not 404 (404 is silently ignored as the thread was already deleted). The service error is wrapped in AgentThreadOperationException. Common for 401/403 (auth), 429 (rate limit), or 5xx (service) failures during deletion.","triggerScenarios":"Calling DeleteAsync on an AzureAIAgentThread and the Azure AI Agents service rejects the delete request with a non-404 HTTP error. Authentication has expired since the thread was created, or the service is rate-limiting or experiencing an outage.","commonSituations":"Azure AD token expired between thread creation and deletion. Rate limiting on thread operations. Service degradation. The thread ID is invalid or belongs to a different resource (not 404 but 403). Network interruption during the delete call.","solutions":["Check the InnerException (RequestFailedException) for the specific HTTP status code.","For 401/403, refresh credentials before attempting deletion.","For 429, retry with backoff.","Note: 404 is already handled gracefully (no-op) — a non-404 error indicates a real problem.","Consider whether the thread was already deleted out-of-band via the Azure portal."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    await thread.DeleteAsync(ct);\n}\ncatch (AgentThreadOperationException ex) when (ex.InnerException is RequestFailedException rfe && rfe.Status == 429)\n{\n    await Task.Delay(TimeSpan.FromSeconds(5), ct);\n    await thread.DeleteAsync(ct);\n}","preventionTips":["Treat thread deletion as best-effort in cleanup paths; log failures but don't block.","Refresh Azure AD tokens before long-running sessions that include cleanup.","Remember 404 is already handled gracefully — only non-404 errors reach this path."],"tags":["azureai-thread","network","service-error","delete","agentthreadoperationexception"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}