DEV Community

Daniel Marin
Daniel Marin

Posted on

I Lost Code Claude Wrote for Me. Then I Found Out Every Session Is Saved. Here's How to Find It.

Where Claude Code stores session history, how to search past sessions by date or keyword, how to recover lost code, and four workflows that make session management effortless.

Claude Code automatically saves the full history of every session: the code it wrote, files it changed, commands it ran, and every message exchanged. This history lives on your machine in a specific directory and is fully searchable.

If you've ever closed a session and realized you needed something from it, the good news is: it's almost certainly still there.

Where Claude Code Stores Session History

All Claude Code session data is stored in the ~/.claude/ directory on your machine. This is your home directory's hidden .claude folder, present on macOS, Linux, and WSL on Windows. Inside it you'll find:

~/.claude/
├── projects/          # One subfolder per project, keyed by directory path
│   └── <project-id>/
│       └── <session-id>.jsonl  # One file per session
├── settings.json      # Your global Claude Code preferences
└── CLAUDE.md          # Your global CLAUDE.md (if set)
Enter fullscreen mode Exit fullscreen mode

Session files use the .jsonl (JSON Lines) format: one JSON object per line, each representing a message, tool call, file change, or command execution in the session. Every session gets its own file, timestamped in the filename.

Windows users: If you're running Claude Code in WSL, the ~/.claude/ directory is inside your WSL home (\\wsl$\Ubuntu\home\yourname\.claude\). If running natively on Windows, check %USERPROFILE%\.claude\.

Viewing Session History With the CLI

The fastest way to view or resume a past session is with the built-in claude CLI. Two flags cover the most common needs:

List recent sessions

# Resume the most recent session in the current project
claude --continue

# Or pick from a list of recent sessions
claude --resume
Enter fullscreen mode Exit fullscreen mode

claude --resume opens an interactive picker showing your recent sessions with timestamps and the first message of each session as a preview. Use arrow keys to navigate, Enter to resume. Claude reloads the full conversation context from the session file. It can see everything that happened, not just a summary.

Continue the most recent session instantly

claude --continue
Enter fullscreen mode Exit fullscreen mode

This picks up exactly where you left off without the picker. Useful when you closed the terminal by accident and want to get back immediately. Claude resumes mid-thought. If it was in the middle of a task, it can see that and pick back up.

Searching Session History Manually

When you know roughly what you're looking for (a specific function name, an error message, a timestamp), you can search the raw session files directly. They're plain text (JSON Lines), so standard tools work:

Search all sessions for a keyword

# Search all sessions for a function name or string
grep -r "generateInvoice" ~/.claude/projects/

# Show the surrounding context (5 lines before and after)
grep -r -C 5 "database migration" ~/.claude/projects/
Enter fullscreen mode Exit fullscreen mode

Find sessions from a specific date

# List session files modified in the last 7 days
find ~/.claude/projects/ -name "*.jsonl" -mtime -7 -ls

# List sessions from a specific project directory
ls -lt ~/.claude/projects/*/*.jsonl | head -20
Enter fullscreen mode Exit fullscreen mode

Extract code blocks from a session

cat ~/.claude/projects/<id>/<session>.jsonl | python3 -c "
import sys, json
for line in sys.stdin:
    obj = json.loads(line)
    if obj.get('type') == 'assistant' and obj.get('content'):
        for block in obj['content']:
            if isinstance(block, dict) and block.get('type') == 'text':
                print(block['text'])
"
Enter fullscreen mode Exit fullscreen mode

This works but gets tedious fast for anything beyond a one-off lookup. The four workflows below automate this into repeatable processes.

Workflow 1: Find and Recover Lost Code

The most common session history need: you closed a session and want something back. Maybe Claude wrote a migration script you never committed. Maybe it refactored a function in a way you want to reference. Maybe the terminal crashed mid-session. The code is in ~/.claude/. You just need to find it.

Describe what you're looking for (the approximate date, a file name, a keyword from the conversation, a function name) and a recovery workflow scans the ~/.claude/ directory, extracts matching sessions, reconstructs the relevant code blocks with their original file paths and context, and presents them ready to copy or re-apply.

"Find the database migration code Claude wrote for me last Tuesday. It was for the users table, adding the email_verified column."

The output: scans session files from that timeframe, finds the migration code block, extracts it with the original file path and surrounding context, and offers to re-apply the change to the current project.

Workflow 2: Log Decisions as They Happen

Session recovery solves the problem of finding past work reactively. A session logging workflow solves it proactively: it automatically detects architectural decision points during a development session and logs them (the decision made, alternatives considered, and the rationale) into a structured, searchable log file alongside your code.

Six months later, when you or a teammate wonders why a certain design choice was made, the answer is in a log file rather than lost with the session.

"Start the session logging protocol for this refactoring session. Log all architectural decisions, including why we chose to extract the payment module separately instead of keeping it inline."

The output: a structured decision log at docs/decisions.md with timestamped entries for each key decision point, alternatives considered, and rationale, updated throughout the session.

Workflow 3: Capture Context for the Next Session

Every time you start a new Claude Code session, you lose the context of the previous one. The new session doesn't know what decisions were made yesterday, what blockers are open, or what was left mid-task. The first ten minutes of every session become re-explaining.

A session capture workflow runs at session end and produces a structured handoff note: key decisions made, open questions with context, completed vs. remaining tasks, and a file change log with rationale. The next session loads this note as its starting context.

"Capture this session for handoff. I'm stopping for the day and want to continue tomorrow without losing context. Summarize decisions, open blockers, and what's left on the auth refactor."

The output: a SESSION_HANDOFF.md with decisions, blockers, completed/remaining tasks, and file change log. Load it in the next session: "Read SESSION_HANDOFF.md and continue from there."

Workflow 4: Visualize Months of Session History

If you use Claude Code heavily, you accumulate months of session history. That history contains a lot of accumulated problem-solving: approaches that worked, dead ends, architectural insights, debugging patterns. It's institutional knowledge that's effectively invisible because it's buried in hundreds of session files.

A mind mapping workflow exports your session history, chunks it into summaries, and generates a visual mind map organized by topic cluster, surfacing recurring themes, key insights, and patterns across sessions. The output is importable into NotebookLM or Obsidian, turning scattered session history into a searchable second brain.

"Map my last 6 months of Claude Code sessions. Cluster by topic area (auth, database, API layer, devops) and surface the key decisions and solutions from each cluster."

Which Approach to Use

I need to get back to where I just left off: Use claude --continue or claude --resume. Built-in, instant, no extra setup needed.

I lost code Claude wrote in a past session: Use the recovery workflow. Describe what you're looking for. It scans ~/.claude/ and extracts the relevant code.

I want to document decisions during a session: Use session logging. Activate it at session start. It auto-logs key decisions throughout.

I'm stopping for the day and want to continue seamlessly tomorrow: Use session capture at session end. Load the handoff note at the start of tomorrow's session.

I want to extract knowledge from months of session history: Use mind mapping. Export, cluster, and import into NotebookLM or Obsidian.

Common Questions

"How long does Claude Code keep session history?"

Session files in ~/.claude/ are kept indefinitely. They don't expire or auto-delete. They only disappear if you manually delete them or clear the directory. On a typical development machine, the ~/.claude/ directory stays manageable in size (a few hundred MB) even after years of use.

"Is my session history private?"

Session files stay on your local machine. They're not synced to Anthropic or any cloud service by default. The content of your sessions (code, file paths, conversation) lives only on the machine where Claude Code is installed.

"Can I view session history on a different machine?"

Sessions are local, so they don't automatically transfer between machines. You can copy the ~/.claude/projects/ directory manually to another machine and Claude Code will recognize the session files there. The session capture workflow is a cleaner alternative for multi-machine setups. It produces a portable markdown file instead of depending on raw session files.

"Can I search sessions for a specific file that was changed?"

Yes. File modification events are logged as tool calls in the session JSONL. The recovery workflow can search specifically for file-change events, making it easy to find "what did Claude do to auth.ts in the session last Thursday" without sifting through conversation text manually.

Getting Started

The ~/.claude/ directory is one of the most underutilized features of Claude Code. Most developers discover it only after losing code they wish they'd saved, and then realize the history was there all along. The built-in --resume and --continue flags cover the simple cases.

I publish free playbooks for all four of the advanced workflows (recovery, logging, handoff, and mind mapping) at claudecodehq.com. Each one is a single file you drop into a project folder. No coding, no configuration.

Originally published on claudecodehq.com

Top comments (0)