github/copilot-sdk · error · RuntimeError

Failed to delete session

Error message

Failed to delete session {session_id}: {error}

What it means

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.

Solutions

  1. Read the server error text embedded in the message and fix the underlying cause it names
  2. Verify the session ID exists (e.g. via get_last_session_id() or get_foreground_session_id()) before deleting
  3. Handle already-deleted sessions as a non-fatal case: catch RuntimeError and treat 'not found' errors as success
  4. Ensure the Copilot CLI server is healthy and updated so session.delete is supported correctly

Example fix

// before
await client.delete_session(stale_id)
// after
try:
    await client.delete_session(stale_id)
except RuntimeError as e:
    if "not found" in str(e):
        logging.info("session already gone: %s", stale_id)
    else:
        raise
Defensive patterns

Strategy: try-catch

Validate before calling

if session_id and session_id in known_active_session_ids:
    await client.delete_session(session_id)

Try / catch

try:
    await client.delete_session(session_id)
except RuntimeError as e:
    if "not found" in str(e).lower():
        logging.info("already deleted: %s", session_id)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/d22783128efde2ad. Report an issue: GitHub.

Appendix: source

Thrown at python/copilot/client.py:3915

        Args:
            session_id: The ID of the session to delete.

        Raises:
            RuntimeError: If the client is not connected or deletion fails.

        Example:
            >>> await client.delete_session("session-123")
        """
        if not self._client:
            raise RuntimeError("Client not connected")

        response = await self._client.request("session.delete", {"sessionId": session_id})

        success = response.get("success", False)
        if not success:
            error = response.get("error", "Unknown error")
            raise RuntimeError(f"Failed to delete session {session_id}: {error}")

        # Remove from local sessions map if present
        with self._sessions_lock:
            session = self._sessions.pop(session_id, None)
        if session is not None:
            session._run_disconnect_callback()

    async def get_last_session_id(self) -> str | None:
        """
        Get the ID of the most recently updated session.

        This is useful for resuming the last conversation when the session ID
        was not stored.

        Returns:
            The session ID, or None if no sessions exist.

        Raises:

View on GitHub (pinned to cd8cf15dc3)