{"record":{"id":"e74f6aef69ba8856","repo":"microsoft/semantic-kernel","slug":"the-thread-could-not-be-deleted-due-to-an-error-re-e74f6a","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/OpenAIResponseAgentThread.cs","lineNumber":84,"sourceCode":"    protected override async Task DeleteInternalAsync(CancellationToken cancellationToken = default)\n    {\n        if (this._isDeleted)\n        {\n            return;\n        }\n\n        if (this.ResponseId is null)\n        {\n            throw new InvalidOperationException(\"This thread cannot be deleted, since it has not been created.\");\n        }\n\n        try\n        {\n            await this._client.DeleteResponseAsync(this.ResponseId, cancellationToken).ConfigureAwait(false);\n        }\n        catch (Exception ex)\n        {\n            throw new AgentThreadOperationException(\"The thread could not be deleted due to an error response from the service.\", ex);\n        }\n\n        this._isDeleted = true;\n    }\n\n    /// <inheritdoc/>\n    protected override Task OnNewMessageInternalAsync(ChatMessageContent newMessage, CancellationToken cancellationToken = default)\n    {\n        if (this._isDeleted)\n        {\n            throw new InvalidOperationException(\"This thread has been deleted and cannot be used anymore.\");\n        }\n\n        return Task.CompletedTask;\n    }\n\n    /// <inheritdoc />\n    public async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/OpenAI/OpenAIResponseAgentThread.cs#L66-L102","documentation":"Thrown by DeleteInternalAsync when client.DeleteResponseAsync(ResponseId) raises any exception. The raw exception is caught and rewrapped in AgentThreadOperationException, so callers get a consistent error type for thread-service failures. This is a network/service error, distinct from the lifecycle checks (249) that precede it.","triggerScenarios":"Calling thread.DeleteAsync() on a valid Response thread while the OpenAI service rejects the delete — auth failure, rate limit, the response was already deleted server-side, network timeout, etc.","commonSituations":"Expired/revoked API key at cleanup time; 429 during teardown; the stored response no longer exists server-side (already purged); intermittent network issues.","solutions":["Inspect ex.InnerException for the underlying error and HTTP status.","Treat 404 (already gone) as success (the resource is effectively deleted).","Retry transient failures (429/5xx) with backoff."],"exampleFix":"// before\nawait thread.DeleteAsync(); // may throw AgentThreadOperationException\n\n// after\ntry { await thread.DeleteAsync(); }\ncatch (AgentThreadOperationException ex)\n{\n    if (ex.InnerException is ClientResultException cre && (int)cre.Status == 404) return; // already gone\n    logger.LogError(ex.InnerException, \"delete failed\");\n    throw;\n}","handlingStrategy":"retry","validationCode":"// Retry transient delete failures with backoff\nstatic async Task SafeDeleteAsync(OpenAIResponseAgentThread thread, int attempts = 3)\n{\n    for (int i = 0; i < attempts; i++)\n    {\n        try { await thread.DeleteAsync(); return; }\n        catch (AgentThreadOperationException ex) when (ex.InnerException is ClientResultException c && (int)c.Status == 404) { return; }\n        catch (AgentThreadOperationException) when (i < attempts - 1) { await Task.Delay(TimeSpan.FromSeconds(Math.Pow(2, i))); }\n    }\n}","typeGuard":null,"tryCatchPattern":"try { await thread.DeleteAsync(); }\ncatch (AgentThreadOperationException ex)\n{\n    if (ex.InnerException is ClientResultException cre && (int)cre.Status == 404) return; // already gone\n    logger.LogError(ex.InnerException, \"delete failed\");\n    throw;\n}","preventionTips":["Treat 404 on delete as success (resource is effectively deleted).","Retry 429/5xx delete failures with exponential backoff.","Log ex.InnerException to capture the real HTTP status."],"tags":["network","service-error","thread","responses-api","agents"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}