Bug: --setting-sources local causes cleanup to ignore cleanupPeriodDays, silently deleting conversations globally
Summary
claude -p --setting-sources local starts background housekeeping that ignores the user's cleanupPeriodDays setting in ~/.claude/settings.json, falling back to the 30-day default. This silently deletes conversation .jsonl files across all projects, not just the current one.
Impact
I lost months of conversation history across dozens of projects. I had cleanupPeriodDays: 1200 set in ~/.claude/settings.json since February 25, but batch claude -p --setting-sources local judging runs triggered cleanup with the 30-day default, wiping all conversations older than ~30 days globally.
The session subdirectories (subagent transcripts, tool-results) survived — only the main .jsonl files were deleted, leaving orphaned directories everywhere.
Root Cause
Traced through decompiled v2.1.63 source (the version running when data loss occurred):
--setting-sources localexcludesuserSettings:getEnabledSettingSources()returns["localSettings", "policySettings", "flagSettings"]—userSettingsis not includedloadSettingsFromDisk()skips~/.claude/settings.json: SinceuserSettingsis not in the enabled list, the file is never read. The merged settings have nocleanupPeriodDays- Cleanup falls back to 30 days:
getCutoffDate()usessettings.cleanupPeriodDays ?? 30 - The safety guard is also defeated:
rawSettingsContainsKey("cleanupPeriodDays")only checks enabled sources, so it doesn't find the key inuserSettings→ guard doesn't trigger claude -pruns background housekeeping:startBackgroundHousekeeping()is called in the non-interactive path, with cleanup firing 10 minutes after session start--no-session-persistencedoesn't help: It only prevents writing new transcripts, it does NOT disable cleanup- Cleanup is global:
cleanupOldSessionFiles()walks~/.claude/projects/and deletes across ALL projects
Reproduction
# Set a long retention
echo '{"cleanupPeriodDays": 9999}' > ~/.claude/settings.json
# This will ignore the 9999 and use 30:
echo "test" | claude -p --setting-sources local --no-session-persistence --model haiku
# If session lives 10+ minutes, cleanup runs with 30-day default
Suggested Fixes
Any of these would prevent the issue:
- Always read
cleanupPeriodDaysfrom user settings for cleanup, regardless of--setting-sources— cleanup is a global operation and should respect global config - Disable background housekeeping in piped mode (
claude -p) — short-lived batch processes shouldn't run global cleanup - Make
rawSettingsContainsKeycheck all settings files, not just enabled sources - Scope cleanup to the current project instead of walking all of
~/.claude/projects/
Environment
- Claude Code v2.1.63 (when data loss occurred), currently on v2.1.89
- Linux (cluster)
- Bug confirmed via decompiled v2.1.63 source code analysis
🤖 Generated with Claude Code
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗