{"record":{"id":"2b6ecf881b9b1f73","repo":"microsoft/semantic-kernel","slug":"thread-has-been-deleted-call-create-to-recrea","errorCode":null,"errorMessage":"Thread has been deleted; call `create()` to recreate it.","messagePattern":"Thread has been deleted; call `create\\(\\)` to recreate it\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/agent.py","lineNumber":123,"sourceCode":"# endregion\n\n\n# region AgentThread\n\n\nclass AgentThread(ABC):\n    \"\"\"Base class for agent threads.\"\"\"\n\n    def __init__(self):\n        \"\"\"Initialize the agent thread.\"\"\"\n        self._is_deleted: bool = False  # type: ignore\n        self._id: str | None = None  # type: ignore\n\n    @property\n    def id(self) -> str | None:\n        \"\"\"Returns the ID of the current thread (if any).\"\"\"\n        if self._is_deleted:\n            raise RuntimeError(\"Thread has been deleted; call `create()` to recreate it.\")\n        return self._id\n\n    async def create(self) -> str | None:\n        \"\"\"Starts the thread and returns the thread ID.\"\"\"\n        # A thread should not be recreated after it has been deleted.\n        if self._is_deleted:\n            raise RuntimeError(\"Cannot create thread because it has already been deleted.\")\n\n        # If the thread ID is already set, we're done, just return the Id.\n        if self.id is not None:\n            return self.id\n\n        # Otherwise, create the thread.\n        self._id = await self._create()\n        return self.id\n\n    async def delete(self) -> None:\n        \"\"\"Ends the current thread.\"\"\"","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/agent.py#L105-L141","documentation":"AgentThread tracks a deleted flag; after delete() sets it, any subsequent read of the .id property raises RuntimeError. A deleted thread is terminal and cannot be queried. The message tells you to create() a new thread — but note the same class instance cannot be recreated (see error 694); create() also refuses on a deleted instance.","triggerScenarios":"Reading thread.id after await thread.delete(); logging/inspecting a thread you already cleaned up; reusing a thread reference past its lifecycle.","commonSituations":"Cleanup-then-use ordering bugs; holding a thread reference in a long-lived object after deletion; double-processing where one path deletes and another reads.","solutions":["Do not access thread.id after delete(); drop the reference and build a fresh thread instance.","Track deletion in your own code (a flag or None-ing the reference) so you never read a deleted thread.","Order your logic so all reads happen before delete().","If you need a new conversation after deletion, construct a new AgentThread subclass instance and call create()."],"exampleFix":"# before\nawait thread.delete()\nprint(thread.id)  # RuntimeError\n# after\nawait thread.delete()\nthread = MyAgentThread()  # new instance\nawait thread.create()\nprint(thread.id)","handlingStrategy":"validation","validationCode":"# Track deletion in your own code; never read .id after delete().\nif getattr(thread, '_is_deleted', False):\n    raise RuntimeError('thread already deleted; build a new instance')\nprint(thread.id)","typeGuard":"def thread_is_deleted(thread) -> bool:\n    return getattr(thread, '_is_deleted', False)","tryCatchPattern":"try:\n    tid = thread.id\nexcept RuntimeError as e:\n    if 'deleted' in str(e).lower():\n        thread = MyAgentThread()  # new instance\n        tid = await thread.create()\n    else:\n        raise","preventionTips":["Read thread.id only before delete().","Null out references after deletion.","Track your own 'deleted' flag in long-lived holders.","Allocate a fresh thread for new conversations."],"tags":["agents","thread","lifecycle","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}