Claude Code Session Management: /clear, /compact, and /rewind Explained
I was in the middle of a complex refactoring task when Claude Code stopped responding with a cryptic error:
Context length exceeded. Please start a new session or reduce context size.My heart sank. I had spent the last hour working through a tricky implementation, and now I was going to lose all that context? Or worse, I’d have to copy-paste everything into a new session?
Turns out, I was doing it wrong. Claude Code has built-in session management commands that I had completely ignored. Let me walk you through what I learned.
The Problem: Context Overflow
When you work with Claude Code for extended periods, the conversation context grows. Every message, every code review, every debugging session gets added to the context window. Eventually, you hit the limit.
Here’s what I tried first (the wrong way):
- Copy-pasting important parts to a notes file
- Starting a new session and re-explaining everything
- Losing track of what code changes were actually made
This was inefficient and error-prone. Then I discovered the three commands that changed everything.
The Solution: Three Essential Commands
1. /compact - Your Daily Driver
/compact is the command I wish I had known about from day one. It compresses your conversation into a summary while preserving the essential information.
/compactWhat it does:
- Summarizes the conversation history
- Keeps key decisions and code changes
- Reduces token usage significantly
- Maintains context continuity
When to use it:
- Every 30-60 minutes during long tasks
- Before starting a new subtask
- When you notice responses getting slower
- Proactively, before you hit context limits
I now run /compact regularly during coding sessions. It’s like taking a checkpoint in a game - you condense the history but keep all the important bits.
2. /clear - The Nuclear Option
/clear is more aggressive. It wipes the conversation history entirely, but crucially, it preserves your code changes.
/clearWhat it does:
- Clears the entire conversation
- Preserves file modifications
- Gives you a fresh context window
- Removes all discussion history
When to use it:
- When the conversation has drifted too far off-topic
- When you want to start fresh but keep code changes
- When
/compactisn’t enough - When you need maximum context space for a new task
The key insight here: /clear doesn’t undo your work. Your code changes remain intact. Only the conversation history is removed.
3. /rewind - The Undo Button
/rewind (or pressing Esc twice quickly) is your safety net when Claude makes changes you didn’t want.
/rewindOr simply:
Esc + EscWhat it does:
- Undoes recent modifications
- Reverts file changes
- Allows you to recover from mistakes
- Works like a time machine for your code
When to use it:
- Claude deleted something it shouldn’t have
- Claude refactored code in a way you dislike
- Claude introduced a bug
- Any time you want to revert recent changes
I’ve saved myself countless times with this command. It’s especially useful when Claude gets a bit too enthusiastic about “improvements” that aren’t actually improvements.
Visualizing the Decision Flow
Here’s a quick decision tree:
┌─────────────────────────────────────┐│ Is your context getting long? │└─────────────────┬───────────────────┘ │ ┌───────▼───────┐ │ YES │ └───────┬───────┘ │ ┌────────────▼────────────┐ │ Is conversation useful? │ └────────────┬─────────────┘ │ ┌───────▼───────┐ │ YES │──────► /compact └───────┬───────┘ │ ┌───────▼───────┐ │ NO │──────► /clear └───────────────┘
┌─────────────────────────────────────┐│ Did Claude make unwanted changes? │└─────────────────┬───────────────────┘ │ ┌───────▼───────┐ │ YES │──────► /rewind └───────────────┘A Real-World Example
Let me show you how I use these commands in practice.
Scenario: I’m implementing a new feature with multiple components.
# Start working on the feature# ... 30 minutes of back-and-forth ...
# Checkpoint - compress the context/compact
# Continue with related work# ... another 30 minutes ...
# Realized Claude took a wrong turn/rewind
# Get back on track# ... more work ...
# Conversation is now about something completely different# but I want to keep the code changes/clear
# Fresh context, same code state# Continue with fresh perspectiveThis workflow has become second nature to me. The key is being proactive rather than reactive.
Why These Commands Matter
Without session management, you face several problems:
- Token waste - Long conversations consume tokens unnecessarily
- Lost context - Starting fresh means re-explaining everything
- No rollback - Unwanted changes require manual reversal
- Slower responses - Larger context means slower processing
These commands solve all of these issues elegantly.
Common Mistakes to Avoid
I made these mistakes so you don’t have to:
Mistake 1: Waiting too long to compact
❌ Wait until context overflow error✅ Run /compact every 30-60 minutesMistake 2: Using /clear when /compact would suffice
❌ /clear (lose all context)✅ /compact (keep summary of context)Mistake 3: Not using /rewind when Claude goes rogue
❌ Manually undo changes line by line✅ /rewind to instantly revertQuick Reference Table
| Command | What It Does | When to Use | Keyboard Shortcut |
|---|---|---|---|
/compact | Summarizes conversation | Every 30-60 min, before context gets too long | None |
/clear | Clears conversation, keeps code | When context is irrelevant or corrupted | None |
/rewind | Undoes recent changes | When Claude makes unwanted modifications | Esc+Esc |
My Recommended Workflow
Based on months of using Claude Code, here’s my recommended workflow:
- Start a task - Begin working on your feature/bug fix
- Work for 30-45 minutes - Let the conversation flow naturally
- Run
/compact- Checkpoint your progress - Continue working - Pick up where you left off
- If Claude makes a mistake - Run
/rewindimmediately - If conversation drifts - Run
/clearand re-explain briefly - Repeat - Make
/compacta habit
The key is to make these commands part of your muscle memory, not emergency measures.
Summary
In this post, I explained Claude Code’s three session management commands. The key point is that /compact should be used regularly to prevent context overflow, /clear resets context while preserving code, and /rewind undoes unwanted changes.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments