Skip to content

Commit 572571d

Browse files
committed
feat: delete_session() also removes {sid}/ subagent transcript dir
1 parent dfbd7d3 commit 572571d

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/claude_agent_sdk/_internal/session_mutations.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import json
2323
import os
2424
import re
25+
import shutil
2526
import unicodedata
2627
import uuid as uuid_mod
2728
from dataclasses import dataclass
@@ -170,11 +171,13 @@ def delete_session(
170171
session_id: str,
171172
directory: str | None = None,
172173
) -> None:
173-
"""Delete a session by removing its JSONL file.
174+
"""Delete a session by removing its JSONL file and subagent transcripts.
174175
175-
This is a hard delete — the file is removed permanently. SDK users who
176-
need soft-delete semantics can use ``tag_session(id, '__hidden')`` and
177-
filter on listing instead.
176+
This is a hard delete — the ``{session_id}.jsonl`` file is removed
177+
permanently, along with the sibling ``{session_id}/`` subdirectory that
178+
holds subagent transcripts (if it exists). SDK users who need soft-delete
179+
semantics can use ``tag_session(id, '__hidden')`` and filter on listing
180+
instead.
178181
179182
Args:
180183
session_id: UUID of the session to delete.
@@ -206,6 +209,8 @@ def delete_session(
206209
if e.errno == errno.ENOENT:
207210
raise FileNotFoundError(f"Session {session_id} not found") from e
208211
raise
212+
# Subagent transcripts live in a sibling {session_id}/ dir; often absent.
213+
shutil.rmtree(path.parent / session_id, ignore_errors=True)
209214

210215

211216
@dataclass

tests/test_session_mutations.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,25 @@ def test_deletes_session_file(self, claude_config_dir: Path, tmp_path: Path):
490490
delete_session(sid, directory=project_path)
491491
assert not file_path.exists()
492492

493+
def test_removes_subagent_transcript_dir(
494+
self, claude_config_dir: Path, tmp_path: Path
495+
):
496+
"""Cascades the sibling {sid}/ subagent dir alongside the .jsonl."""
497+
project_path = str(tmp_path / "proj")
498+
Path(project_path).mkdir(parents=True)
499+
project_dir = _make_project_dir(
500+
claude_config_dir, os.path.realpath(project_path)
501+
)
502+
sid, file_path = _make_session_file(project_dir)
503+
subagent_dir = project_dir / sid
504+
subagent_dir.mkdir()
505+
(subagent_dir / f"{uuid.uuid4()}.jsonl").write_text("{}\n")
506+
507+
delete_session(sid, directory=project_path)
508+
509+
assert not file_path.exists()
510+
assert not subagent_dir.exists()
511+
493512
def test_deletes_without_directory(self, claude_config_dir: Path):
494513
"""Searches all project directories when no directory is given."""
495514
project_dir = _make_project_dir(claude_config_dir, "/any/project")

0 commit comments

Comments
 (0)