anthropics / anthropics/claude-agent-sdk-python

delete_session can report success while leaving subagent transcripts behind

Open
#1,164 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
8.1k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### Summary

The filesystem `delete_session()` API silently ignores every error while deleting the sibling subagent transcript directory, even though its documentation promises a permanent hard delete of both locations.

### Affected code

`src/claude_agent_sdk/_internal/session_mutations.py:182-229`

### Current behavior

The main `{session_id}.jsonl` file is unlinked first. The function then runs:

```python
shutil.rmtree(path.parent / session_id, ignore_errors=True)
```

This suppresses permission errors, read-only filesystem errors, transient I/O failures, and any other failure. The function returns normally after partial deletion.

### Why this matters

The sibling directory contains subagent transcripts and metadata, which may include prompts, tool inputs/results, paths, and other sensitive information. A caller using a documented hard-delete API receives no indication that this data remains.

Because the main file has already been removed, retrying via the same API raises `FileNotFoundError`, making cleanup harder.

### Expected behavior

The function should return success only when the main transcript and associated subagent directory are absent. Failure to remove material data should be reported.

### Possible fix

- Ignore only `ENOENT` for an absent sibling directory.
- Propagate other removal failures.
- Consider deleting the subagent directory first, or otherwise define recoverable partial-failure behavior.
- Add a test that simulates `rmtree` raising `PermissionError` and verifies that the caller sees it.

The docstring's Raises section should include relevant `OSError` behavior.

### Environment

- Repository revision: current `main` audit at SDK version 0.2.128
- Bundled CLI version: 2.1.220
- Python test suite: 1,291 passed, 5 skipped
- Ruff and mypy: clean

I searched the existing issues and pull requests using the affected symbols and behavior before filing this.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.