[BUG] Session cleanup (cleanupPeriodDays) does not delete subagent files

Resolved 💬 2 comments Opened Apr 14, 2026 by srangwal Closed Apr 17, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

## Bug Description

The session retention cleanup (cleanupPeriodDays setting) correctly deletes expired main session .jsonl files, but does not clean up
subagent session files
stored in <session-id>/subagents/ directories. This causes unbounded disk usage growth over time.

## Steps to Reproduce

  1. Set "cleanupPeriodDays": 5 in ~/.claude/settings.json
  2. Use Claude Code with the Agent tool (subagents) over several weeks
  3. After 5+ days, check for stale files:

```bash
find ~/.claude/projects/ -path "/subagents/" -type f -mtime +5 | wc -l

Expected: 0 files (all cleaned up)
Actual: Hundreds/thousands of orphan subagent files persist indefinitely

Root Cause

The session cleanup function (wm1 in the minified source) iterates over ~/.claude/projects/<project>/ entries. For session directories, it
only descends into two hardcoded subdirectory names:

  1. tool-results
  2. frame

The subagents subdirectory is not handled. After cleaning tool-results and frame, the code calls rmdir() on the session directory — but
since subagents/ still contains files, rmdir silently fails (error is caught and discarded), and the entire directory tree persists.

Impact

On my machine over ~75 days of usage:

  • 888 orphan subagent files accumulated (.jsonl + .meta.json)
  • 117 MB of wasted disk space
  • Files dating back to January 2026 despite a 5-day cleanup period
  • 95 orphan session directories that can never be removed by the current cleanup logic

Heavy subagent users (using Agent tool, teams, parallel agents) will be disproportionately affected.

Suggested Fix

In the session directory cleanup branch, add equivalent cleanup for the subagents subdirectory:

// pseudocode — after handling tool-results and frame:
const subagentsDir = path.join(sessionDir, "subagents");
for (const entry of await readdir(subagentsDir).catch(() => [])) {
if (entry.isFile()) {
await deleteIfOld(path.join(subagentsDir, entry.name), cutoffDate);
}
}
await rmdirIfEmpty(subagentsDir);

Workaround

Manual cleanup:
find ~/.claude/projects/ -path "/subagents/" -type f -mtime +30 -delete
find ~/.claude/projects/ -type d -name "subagents" -empty -delete
find ~/.claude/projects/ -mindepth 2 -maxdepth 2 -type d -empty -delete

Environment

  • Claude Code version: 2.1.105
  • Platform: Linux (Azure Mariner)
  • Setting: cleanupPeriodDays: 5

What Should Happen?

Subagent session files stored in <session-id>/subagents/ should be deleted when they exceed the cleanupPeriodDays threshold, just like
main session .jsonl files and tool-results files are.

Error Messages/Logs

Steps to Reproduce

## Steps to Reproduce

  1. Set "cleanupPeriodDays": 5 in ~/.claude/settings.json
  2. Use Claude Code with the Agent tool (subagents) over several weeks
  3. After 5+ days, check for stale files:

```bash
find ~/.claude/projects/ -path "/subagents/" -type f -mtime +5 | wc -l

Expected: 0 files (all cleaned up)
Actual: Hundreds/thousands of orphan subagent files persist indefinitely

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

2.1.105

Platform

Anthropic API

Operating System

Other Linux

Terminal/Shell

Other

Additional Information

Claude code helped me identify the issue and the root cause of it. Everything in the ticket is directly from Claude code

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗