{"record":{"id":"d22783128efde2ad","repo":"github/copilot-sdk","slug":"failed-to-delete-session-session-id-error","errorCode":null,"errorMessage":"Failed to delete session {session_id}: {error}","messagePattern":"Failed to delete session (.+?): (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/copilot/client.py","lineNumber":3915,"sourceCode":"\n        Args:\n            session_id: The ID of the session to delete.\n\n        Raises:\n            RuntimeError: If the client is not connected or deletion fails.\n\n        Example:\n            >>> await client.delete_session(\"session-123\")\n        \"\"\"\n        if not self._client:\n            raise RuntimeError(\"Client not connected\")\n\n        response = await self._client.request(\"session.delete\", {\"sessionId\": session_id})\n\n        success = response.get(\"success\", False)\n        if not success:\n            error = response.get(\"error\", \"Unknown error\")\n            raise RuntimeError(f\"Failed to delete session {session_id}: {error}\")\n\n        # Remove from local sessions map if present\n        with self._sessions_lock:\n            session = self._sessions.pop(session_id, None)\n        if session is not None:\n            session._run_disconnect_callback()\n\n    async def get_last_session_id(self) -> str | None:\n        \"\"\"\n        Get the ID of the most recently updated session.\n\n        This is useful for resuming the last conversation when the session ID\n        was not stored.\n\n        Returns:\n            The session ID, or None if no sessions exist.\n\n        Raises:","sourceCodeStart":3897,"sourceCodeEnd":3933,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/python/copilot/client.py#L3897-L3933","documentation":"delete_session() sends the 'session.delete' JSON-RPC request to the Copilot CLI server. If the server response lacks success=true, the client raises this RuntimeError including the server-provided error string. It means the server refused or failed to delete the named session.","triggerScenarios":"Calling await client.delete_session(session_id) when the server responds with success=false, e.g. the session ID no longer exists on the server, or the server reports an internal error via the 'error' field of the response.","commonSituations":"Deleting a session that already terminated or was deleted elsewhere; a stale session ID from a previous server run; server-side storage errors; passing a fabricated or mistyped session ID.","solutions":["Read the server error text embedded in the message and fix the underlying cause it names","Verify the session ID exists (e.g. via get_last_session_id() or get_foreground_session_id()) before deleting","Handle already-deleted sessions as a non-fatal case: catch RuntimeError and treat 'not found' errors as success","Ensure the Copilot CLI server is healthy and updated so session.delete is supported correctly"],"exampleFix":"// before\nawait client.delete_session(stale_id)\n// after\ntry:\n    await client.delete_session(stale_id)\nexcept RuntimeError as e:\n    if \"not found\" in str(e):\n        logging.info(\"session already gone: %s\", stale_id)\n    else:\n        raise","handlingStrategy":"try-catch","validationCode":"if session_id and session_id in known_active_session_ids:\n    await client.delete_session(session_id)","typeGuard":null,"tryCatchPattern":"try:\n    await client.delete_session(session_id)\nexcept RuntimeError as e:\n    if \"not found\" in str(e).lower():\n        logging.info(\"already deleted: %s\", session_id)\n    else:\n        raise","preventionTips":["Track session IDs you created and only delete live ones","Treat delete of an already-gone session as idempotent success","Keep the CLI server updated to avoid spurious delete failures"],"tags":["runtime-error","session-management","rpc","python"],"backgroundTag":"api-error-response","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}