Skip to content

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:

Error output
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):

  1. Copy-pasting important parts to a notes file
  2. Starting a new session and re-explaining everything
  3. 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.

Compact command
/compact

What 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.

Clear command
/clear

What 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 /compact isn’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.

Rewind command
/rewind

Or simply:

Keyboard shortcut
Esc + Esc

What 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:

Session management decision flow
┌─────────────────────────────────────┐
│ 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.

Session workflow example
# 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 perspective

This 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:

  1. Token waste - Long conversations consume tokens unnecessarily
  2. Lost context - Starting fresh means re-explaining everything
  3. No rollback - Unwanted changes require manual reversal
  4. 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

Wrong approach
❌ Wait until context overflow error
✅ Run /compact every 30-60 minutes

Mistake 2: Using /clear when /compact would suffice

Wrong command choice
❌ /clear (lose all context)
✅ /compact (keep summary of context)

Mistake 3: Not using /rewind when Claude goes rogue

Wrong fix method
❌ Manually undo changes line by line
✅ /rewind to instantly revert

Quick Reference Table

CommandWhat It DoesWhen to UseKeyboard Shortcut
/compactSummarizes conversationEvery 30-60 min, before context gets too longNone
/clearClears conversation, keeps codeWhen context is irrelevant or corruptedNone
/rewindUndoes recent changesWhen Claude makes unwanted modificationsEsc+Esc

Based on months of using Claude Code, here’s my recommended workflow:

  1. Start a task - Begin working on your feature/bug fix
  2. Work for 30-45 minutes - Let the conversation flow naturally
  3. Run /compact - Checkpoint your progress
  4. Continue working - Pick up where you left off
  5. If Claude makes a mistake - Run /rewind immediately
  6. If conversation drifts - Run /clear and re-explain briefly
  7. Repeat - Make /compact a 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