{"record":{"id":"a231c25d0561cf3d","repo":"microsoft/semantic-kernel","slug":"this-thread-has-been-deleted-and-cannot-be-used-an-a231c2","errorCode":null,"errorMessage":"This thread has been deleted and cannot be used anymore.","messagePattern":"This thread has been deleted and cannot be used anymore\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/AzureAI/AzureAIAgentThread.cs","lineNumber":156,"sourceCode":"            {\n                throw new AgentThreadOperationException(ErrorMessage, ex);\n            }\n        }\n    }\n\n    /// <summary>\n    /// Asynchronously retrieves all messages in the thread.\n    /// </summary>\n    /// <param name=\"sortOrder\">The order to return messages in.</param>\n    /// <param name=\"cancellationToken\">The <see cref=\"CancellationToken\"/> to monitor for cancellation requests. The default is <see cref=\"CancellationToken.None\"/>.</param>\n    /// <returns>The messages in the thread.</returns>\n    /// <exception cref=\"InvalidOperationException\">The thread has been deleted.</exception>\n    [Experimental(\"SKEXP0110\")]\n    public async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync(ListSortOrder? sortOrder = default, [EnumeratorCancellation] 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        if (this.Id is null)\n        {\n            await this.CreateAsync(cancellationToken).ConfigureAwait(false);\n        }\n\n        await foreach (var message in AgentThreadActions.GetMessagesAsync(this._client, this.Id!, sortOrder, cancellationToken).ConfigureAwait(false))\n        {\n            yield return message;\n        }\n    }\n}\n","sourceCodeStart":138,"sourceCodeEnd":170,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/AzureAI/AzureAIAgentThread.cs#L138-L170","documentation":"Thrown by AzureAIAgentThread.GetMessagesAsync when IsDeleted is true. This is the Azure AI-specific message retrieval method that enumerates all messages in a thread. After DeleteAsync has completed (IsDeleted = true), calling GetMessagesAsync is rejected because the underlying thread no longer exists on the service.","triggerScenarios":"Calling azureThread.GetMessagesAsync() after the thread has been deleted via DeleteAsync. The method checks IsDeleted before attempting lazy creation or message retrieval.","commonSituations":"Fetching message history for logging/display after the conversation cleanup has already run. Caching the thread reference and calling GetMessagesAsync in a post-session report. Race condition between a cleanup task and a history-retrieval task sharing the same thread reference.","solutions":["Retrieve messages before calling DeleteAsync.","Guard with: if (!thread.IsDeleted) { await foreach (var m in thread.GetMessagesAsync()) {...} }","Cache messages locally before deletion if post-deletion access is needed."],"exampleFix":"// before\nawait thread.DeleteAsync(ct);\nawait foreach (var msg in thread.GetMessagesAsync()) { ... } // throws\n\n// after\nawait foreach (var msg in thread.GetMessagesAsync()) { history.Add(msg); }\nawait thread.DeleteAsync(ct);","handlingStrategy":"validation","validationCode":"if (!azureThread.IsDeleted)\n{\n    await foreach (var msg in azureThread.GetMessagesAsync(cancellationToken: ct))\n    {\n        messages.Add(msg);\n    }\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await foreach (var msg in thread.GetMessagesAsync(cancellationToken: ct))\n    {\n        history.Add(msg);\n    }\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"has been deleted\"))\n{\n    _logger.LogWarning(\"Cannot retrieve messages: thread was deleted.\");\n}","preventionTips":["Always retrieve messages before deletion.","Guard every GetMessagesAsync call with an IsDeleted check.","Cache messages locally if they are needed after cleanup."],"tags":["azureai-thread","lifecycle","getmessages","invalidoperation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}