{"record":{"id":"916d05f86f7e727e","repo":"microsoft/semantic-kernel","slug":"the-thread-could-not-be-deleted-due-to-an-error-re-916d05","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/Bedrock/BedrockAgentThread.cs","lineNumber":86,"sourceCode":"            var endSessionResponse = await this._runtimeClient.EndSessionAsync(\n                request: new()\n                {\n                    SessionIdentifier = this.Id\n                },\n                cancellationToken: cancellationToken).ConfigureAwait(false);\n\n            var deleteSessionResponse = await this._runtimeClient.DeleteSessionAsync(\n                request: new()\n                {\n                    SessionIdentifier = this.Id\n                },\n                cancellationToken: cancellationToken).ConfigureAwait(false);\n\n            this.Id = null;\n        }\n        catch (AmazonBedrockAgentRuntimeException 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        // Create the thread if it does not exist. Bedrock agents cannot add messages to the thread without invoking so we don't do that here\n        await this.CreateAsync(cancellationToken).ConfigureAwait(false);\n    }\n}\n","sourceCodeStart":68,"sourceCodeEnd":101,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Bedrock/BedrockAgentThread.cs#L68-L101","documentation":"Thrown during thread deletion when DeleteSessionAsync on the Bedrock Agent runtime returns an AmazonBedrockAgentRuntimeException. The original AWS exception is preserved as InnerException inside AgentThreadOperationException so callers can catch a single type for all thread lifecycle errors while still accessing the AWS-specific error code.","triggerScenarios":"Calling DeleteAsync on a BedrockAgentThread whose Id refers to a session the AWS service refuses to delete — missing `bedrock-agent-runtime:DeleteSession` permission, the session was already deleted/expired, or the service is throttling.","commonSituations":"IAM policy grants Create but not Delete; attempting to delete a session whose identifier has already expired or been removed; cross-account access without delete permission; throttling under heavy load.","solutions":["Inspect InnerException (AmazonBedrockAgentRuntimeException).ErrorCode to distinguish permission denied, not-found, and throttling.","Grant `bedrock-agent-runtime:DeleteSession` in the IAM policy for the principal.","If the session is already gone, treat the error as non-fatal (log and continue) rather than failing the workflow.","Retry transient throttling with backoff."],"exampleFix":"// before\ntry { await thread.DeleteAsync(cancellationToken); }\ncatch (Exception) { /* swallow, hides real cause */ }\n\n// after\ntry { await thread.DeleteAsync(cancellationToken); }\ncatch (AgentThreadOperationException ex) when (ex.InnerException is AmazonBedrockAgentRuntimeException aws)\n{\n    if (aws.ErrorCode == \"ResourceNotFoundException\") return; // already gone\n    logger.LogError(aws, \"Session delete failed: {ErrorCode}\", aws.ErrorCode);\n    throw;\n}","handlingStrategy":"try-catch","validationCode":"// Confirm the session id still exists before deletion (best-effort)\nif (string.IsNullOrEmpty(thread.Id)) { /* nothing to delete */ return; }","typeGuard":null,"tryCatchPattern":"try { await thread.DeleteAsync(cancellationToken); }\ncatch (AgentThreadOperationException ex) when (ex.InnerException is AmazonBedrockAgentRuntimeException aws)\n{\n    if (aws.ErrorCode == \"ResourceNotFoundException\") { logger.LogInformation(\"Session already gone.\"); return; }\n    logger.LogError(aws, \"DeleteSession failed: {Code}\", aws.ErrorCode);\n    throw;\n}","preventionTips":["Grant `bedrock-agent-runtime:DeleteSession` in the IAM policy.","Treat ResourceNotFoundException during delete as non-fatal (idempotent cleanup).","Retry transient throttling with backoff rather than failing the whole workflow."],"tags":["bedrock","agents","session","aws-service-error","csharp"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}