{"record":{"id":"86a0066ac0900cf5","repo":"microsoft/semantic-kernel","slug":"the-thread-could-not-be-deleted-due-to-an-error-re-86a006","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/OpenAI/OpenAIAssistantAgentThread.cs","lineNumber":144,"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.DeleteThreadAsync(this.Id, cancellationToken).ConfigureAwait(false);\n        }\n        catch (ClientResultException ex) when (ex.Status == 404)\n        {\n            // Do nothing, since the thread was already deleted.\n        }\n        catch (ClientResultException 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 AssistantThreadActions.CreateMessageAsync(this._client, this.Id!, newMessage, cancellationToken).ConfigureAwait(false);","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/OpenAI/OpenAIAssistantAgentThread.cs#L126-L162","documentation":"DeleteInternalAsync wraps DeleteThreadAsync. A 404 ClientResultException is intentionally swallowed (thread already gone), but any other ClientResultException (auth, rate limit, server error) is rethrown as AgentThreadOperationException.","triggerScenarios":"Deleting a thread that exists but the service returns a non-404 error: expired auth, rate limit, 5xx, or invalid id format.","commonSituations":"Expired credential during cleanup; rate-limited deletion; transient server error; attempting deletion on an already-corrupted thread.","solutions":["Catch AgentThreadOperationException and inspect the ClientResultException.Status from the inner exception.","Retry transient (429/5xx) deletions with backoff.","Refresh credentials if Status is 401/403.","Treat cleanup errors as non-fatal where appropriate, since the thread may already be gone."],"exampleFix":"// before\nawait thread.DeleteAsync();\n// after\ntry { await thread.DeleteAsync(); }\ncatch (AgentThreadOperationException ex) when (ex.InnerException is ClientResultException cre && (cre.Status == 429 || cre.Status >= 500)) {\n    await Task.Delay(backoff);\n    await thread.DeleteAsync();\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await thread.DeleteAsync(); }\ncatch (AgentThreadOperationException ex) when (ex.InnerException is ClientResultException cre && (cre.Status == 429 || cre.Status >= 500)) {\n    await Task.Delay(backoff);\n    await thread.DeleteAsync();\n}","preventionTips":["Treat thread deletion as best-effort during teardown (404 is already swallowed).","Retry transient 429/5xx deletes with backoff.","Refresh credentials if you see 401/403 during cleanup."],"tags":["openai","agent-thread","network","service-error"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}