claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[BUG] `.claude.json` is trucated directly, and so occasionally gets corrupted

Open max-sixty opened this issue 6 months ago • 2 comments

Environment

  • Platform (select one):
    • [x] Anthropic API
    • [ ] AWS Bedrock
    • [ ] Google Vertex AI
    • [ ] Other:
  • Claude CLI version: 1.0.35
  • Operating System: macOS
  • Terminal: Alacritty

Bug Description

I occasionally get my .claude.json wiped. I think that's happening because claude truncates it directly when writing an edit. and so if one process hasn't completed writing when another attempts to read, it'll get a corrupted file, and then wipes the whole thing

this becomes really evident when mounting it as a file to a container; if lots of claudes are running, and .claude.json is large, the container is fairly likely to get less than a full file given docker's mount performance

Expected Behavior

have claude do an atomic rename of .claude.json

max-sixty avatar Jun 25 '25 22:06 max-sixty

is this why mine is full of 'pasted comments' ?

Image

it eventually becomes unusable.

jezweb avatar Jun 27 '25 03:06 jezweb

yeah this happens to me multiple times a day during regular usage (working on 2-3 code bases)

it's sort of upsetting that a conversation history/state is stored here where my settings and mcps are also configured....

elee1766 avatar Jun 27 '25 17:06 elee1766

No update?

pipedexplumbing avatar Jul 02 '25 13:07 pipedexplumbing

ive not seen it happen again but ive also not let claude touch the file except for the single terminal prompt command for injecting a new mcp server. ie, i never ask it to do anything to the file.

jezweb avatar Jul 03 '25 05:07 jezweb

Same here. I tried setting up a claude slash command that uses the !command syntax to run a script to fetch and summarize all my todo comments in parallel before handing over to the interactive instance, but running it once corrupts the .claude.json file multiple times...

which isn't just annoying because it kills my settings, it's annoying because it clutters up the summary with more Claude configuration file at /home/liu/.claude.json is corrupted errors than actual summaries.

I don't even need atomic writes, just a flag to run non-interactive claude (claude -p) without permission to edit the .claude.json file

Liu-Eroteme avatar Jul 04 '25 12:07 Liu-Eroteme

Not sure if there's a new bug with this, but for me it just started this week.

% ls -latr .claude.json*
-rw-r--r--  1 ask  staff   980305 Jul  1 10:00 .claude.json.corrupted.1751389239208
-rw-r--r--  1 ask  staff   977247 Jul  1 19:48 .claude.json.corrupted.1751424481835
-rw-r--r--  1 ask  staff   976846 Jul  1 19:48 .claude.json.corrupted.1751424482017
-rw-r--r--  1 ask  staff   968884 Jul  1 22:31 .claude.json.corrupted.1751434288805
-rw-r--r--  1 ask  staff   936743 Jul  2 00:04 .claude.json.corrupted.1751439890877
-rw-r--r--  1 ask  staff   936874 Jul  2 00:15 .claude.json.corrupted.1751440514983
-rw-r--r--  1 ask  staff   936874 Jul  2 00:15 .claude.json.corrupted.1751440515066
-rw-r--r--  1 ask  staff   936874 Jul  2 00:15 .claude.json.corrupted.1751440515299
-rw-r--r--  1 ask  staff   730546 Jul  3 21:45 .claude.json.corrupted.1751604331479
-rw-r--r--  1 ask  staff   580402 Jul  4 00:49 .claude.json.corrupted.1751615384768
-rw-r--r--  1 ask  staff   624408 Jul  4 11:24 .claude.json.corrupted.1751653468680
-rw-r--r--  1 ask  staff   601032 Jul  4 11:52 .claude.json.corrupted.1751655143464
-rw-r--r--  1 ask  staff   601920 Jul  4 18:16 .claude.json.corrupted.1751678176115
-rw-r--r--  1 ask  staff        0 Jul  5 13:29 .claude.json.corrupted.1751747390118
-rw-r--r--  1 ask  staff        0 Jul  5 15:54 .claude.json.corrupted.1751756063643
-rw-r--r--  1 ask  staff  3929299 Jul  5 16:48 .claude.json.corrupted.1751759307055
-rw-r--r--  1 ask  staff  3929299 Jul  5 16:48 .claude.json.corrupted.1751759327534
-rw-r--r--  1 ask  staff  4123299 Jul  5 18:59 .claude.json.corrupted.1751767150687
-rw-r--r--  1 ask  staff  4028822 Jul  6 04:19 .claude.json.corrupted.1751800751712
-rw-r--r--  1 ask  staff  4030997 Jul  6 12:34 .claude.json.corrupted.1751830489178
-rw-r--r--  1 ask  staff        0 Jul  6 21:24 .claude.json.corrupted.1751862259470
-rw-r--r--  1 ask  staff  4049065 Jul  6 22:15 .claude.json.corrupted.1751865324414
-rw-r--r--  1 ask  staff        0 Jul  6 22:15 .claude.json.corrupted.1751865324696
-rw-r--r--  1 ask  staff  4052332 Jul  6 22:30 .claude.json.corrupted.1751866201455
-rw-r--r--  1 ask  staff        0 Jul  6 22:30 .claude.json.corrupted.1751866201509
-rw-r--r--  1 ask  staff  4052332 Jul  6 22:30 .claude.json.corrupted.1751866201511
-rw-r--r--  1 ask  staff       99 Jul  6 22:30 .claude.json.backup
-rw-r--r--  1 ask  staff      179 Jul  6 22:30 .claude.json

For what it's worth, the files I spot checked were all valid JSON according to jq.

abh avatar Jul 07 '25 05:07 abh

i ended up deleting a pile of files like that, not sure maybe from claude crashing?

jezweb avatar Jul 07 '25 11:07 jezweb

me too. they appeared for me on reboot when i guess i restarted my computer without closing every claude code session gracefully.

i dont really care that much about losing like the prompt history. maybe being able to set mcps in settings would make it okay? https://docs.anthropic.com/en/docs/claude-code/settings#settings-files

elee1766 avatar Jul 07 '25 21:07 elee1766

Whenever I have Claude Code doing something heavy, it can eat A LOT of disk space. Running out of disk space is one reason that causes an immediate panic & results in .claude.json being wiped. The next time you start Claude it complains that it's corrupt and wants you to setup from scratch again.

juburr avatar Jul 17 '25 21:07 juburr

Whenever I have Claude Code doing something heavy, it can eat A LOT of disk space. Running out of disk space is one reason that causes an immediate panic & results in .claude.json being wiped. The next time you start Claude it complains that it's corrupt and wants you to setup from scratch again.

definitely not that - I ran into this issue on a 2tb partition that was maybe 10% full

.. I'm pretty sure it's parallel claude code instances - but it might've been fixed, because I haven't run into it since tbh - might try to reproduce on monday

Liu-Eroteme avatar Jul 25 '25 19:07 Liu-Eroteme

.. I'm pretty sure it's parallel claude code instances - but it might've been fixed, because I haven't run into it since tbh - might try to reproduce on monday

i think what was causing it for me is that i would kill every claude code instance at once with a killall command after being done working in parallel between 3-4 sessions, and them all trying to save at once broke things, but this also hasnt happened to me in a while so maybe its fixed and we can all rejoyce

elee1766 avatar Jul 25 '25 19:07 elee1766

Used claude code to clear it. By asking to clean the file because it was too large.


Your .claude.json file is 101 MB and contains 90 projects, all of which are:
     - Unnamed projects
     - Have no timestamp information
     - Appear to be empty or orphaned project entries

     Safe Cleanup Strategy:

     1. Create a backup of the current file
     2. Remove all 90 unnamed/empty projects while preserving:
       - All configuration settings (user ID, MCP servers, etc.)
       - Any named projects (currently none found)
       - All non-project data
     3. Verify the cleaned file works correctly
     4. Keep the backup for 30 days as a safety measure

     Expected Results:

     - File size reduction: From 101 MB to ~0.03 MB (99.9% reduction)
     - No loss of important settings or active projects
     - Claude Code will continue working normally

     Steps I'll execute:

     1. Create timestamped backup: .claude.json.backup-YYYYMMDD
     2. Create cleaned version removing all unnamed/empty projects
     3. Replace original file with cleaned version
     4. Verify file integrity and Claude Code functionality

Still don't know what causes it

jezweb avatar Aug 23 '25 01:08 jezweb