Recover Lost Code from Claude Code Sessions
Lost code when a Claude Code session ended? A recovery workflow that scans the session files in ~/.claude/, finds the code Claude wrote by date or keyword, and re-applies it to your project.
Every Claude Code session — the code it wrote, files it changed, commands it ran — is automatically saved to ~/.claude/ on your machine. This playbook shows you exactly how to navigate that history, search by date or keyword, and recover any snippet of work you thought was gone.
Who it's for: developers who lost code changes when a Claude Code session ended unexpectedly, programmers wanting to find a specific solution Claude generated in a past conversation, team members recovering Claude-generated code that was never committed to version control, power users building searchable archives of their Claude Code interaction history, developers debugging issues by reviewing what Claude changed in previous sessions
Example
"Find the database migration code Claude wrote for me last Tuesday" → Recovery pipeline: scan Claude Code session history files (~/.claude/) for matching timeframe, search conversation logs for code blocks and file modifications, extract and reconstruct the specific code changes, present recovered snippets with file paths and context, and optionally re-apply the changes to current project
CLAUDE.md Template
New here? 3-minute setup guide → | Already set up? Copy the template below.
npx ccplaybooks add session-history-recovery# Session History Recovery
## Your Role
You help search through Claude Code session history to recover code, files, or solutions from previous sessions that you may have forgotten to save.
## Session History Location
```markdown
## Where Sessions Are Stored
### macOS/Linux
~/.claude/projects/[project-hash]/sessions/Get new playbooks like this one
One email a week with new Claude Code workflows. Free, like everything here.
No spam. Unsubscribe anytime.
What This Does
Every Claude Code session — including all code written, files changed, and commands run — is automatically saved to a local directory on your machine. This playbook gives Claude Code a structured way to navigate that history, search it by date or keyword, and extract specific pieces of work you need to recover.
Where Claude Code Stores Session History
Claude Code saves every session as a JSON file containing the full conversation, tool calls, and responses.
macOS / Linux:
~/.claude/projects/[project-hash]/sessions/
Windows:
%USERPROFILE%\.claude\projects\[project-hash]\sessions\
Each session file includes:
- Complete conversation history
- All tool calls (file reads, writes, edits)
- All bash commands that were run
- Timestamps for every action
Quick Start
Step 1: Download the Template
Click Download above and place the CLAUDE.md file in any working folder, then open Claude Code in that folder.
Step 2: Describe What You're Looking For
Give Claude as much context as you can:
- When (approximately) the work happened
- What files were involved
- Keywords from the conversation
- The problem you were solving
Step 3: Search and Recover
Start with a broad search and narrow down:
Find the session where I created [file/solution]
What did I work on last Tuesday?
Search for code that handles [description]
Search Strategies
By date:
What did I work on yesterday?
Find sessions from this week
Show me everything I did on [specific date]
By content:
Find the function that handles authentication
Search for sessions mentioning [library name]
Find where I fixed the [error message]
By file:
When did I create utils.ts?
Find sessions where I modified package.json
Which session created the database schema?
By problem:
Find that bug I fixed with the API timeout
Search for my solution to the CORS error
Find the caching implementation I wrote
Common Recovery Scenarios
Recovering a Config File
If you need to find configuration you set up in a previous session:
Search for sessions containing config file patterns (.json, .yaml, .env)
Look for Write tool calls that created settings files
Find where I configured [service name]
Recovering a Script
Find sessions where I created a bash script
Search for sessions with automation or cron keywords
Find the script that processed [input/output description]
Recovering a Bug Fix
Search for sessions containing [the error message]
Find where I fixed the bug in [file name]
Look for Edit tool calls on [file] with fix-related context
Session Export Options
Once you locate the session, you can export:
- Full export — complete conversation, all tool calls, all responses
- Code only — extract just the code blocks written
- File changes only — show only what was created or modified
- Commands only — list all bash commands that were run
- Solutions summary — key decisions and problems solved
Tips for Making Sessions Findable in the Future
- Save important work immediately — copy critical code to committed files, don't rely on history alone
- Use descriptive file names — makes keyword search far more effective
- Mention project names — give Claude context about which project you're in
- Note what problem you're solving — future searches by problem description are the most reliable
- Export key sessions — for critical solutions, export a markdown summary to your notes