[FEATURE] Add --keep flag to /clear command to preserve last N messages

Status Fixed / completed
Maintainer reply None cached
Activity 4 comments · opened Dec 24, 2025 · closed Dec 24, 2025

Problem Statement

When working on long sessions, /clear removes all context including recent relevant exchanges. /compact only summarizes but doesn't reduce context noise. Users need a middle ground - the ability to reset context while keeping the last few messages that are immediately relevant to their current task.

Proposed Solution

Add an optional --keep=N parameter to /clear:

/clear --keep=5    # Clear all but last 5 messages
/clear -k 5        # Short form

Each message (user or assistant) counts as 1, preserving complete message structure including tool uses.

Alternative Solutions

  • Using /compact (doesn't reduce noise, only compresses)
  • Manual copy-paste of recent context (error-prone, tedious)
  • Starting new session and re-explaining context (wastes tokens)

Use Case Example

  1. Working on a complex debugging session for 30 minutes
  2. Context has grown large with many tangential tool calls
  3. Finally isolated the bug in the last 2-3 exchanges
  4. Want to clear old noise but keep the recent debugging context
  5. /clear --keep=5 preserves last 5 messages, clears everything else
  6. Continue working with clean context and relevant recent history

Implementation Considerations

  • Message Counting: Count individual messages (each user or assistant = 1)
  • Tool Results: Preserve complete message structure including tool use blocks
  • System Messages: Only user/assistant messages count toward N
  • Transcript Handling: Create new transcript with preserved messages
  • Hooks Compatibility: transcript_path should point to new transcript

View original on GitHub ↗

4 Comments

github-actions[bot] · 9 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/7919
  2. https://github.com/anthropics/claude-code/issues/2968
  3. https://github.com/anthropics/claude-code/issues/6390

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

douglance · 9 months ago

Related context management issues that this could help address:

  • #9118 - Programmatic context clearing from hooks
  • #10035 - Context reset behavior

A --keep flag would give users more granular control over context management, complementing fixes to the existing /clear behavior.

douglance · 9 months ago

Closing as duplicate of #6390 which proposes the same feature (/prune --keep-last N). Adding support there would be more impactful.

The core request is identical: keep last N messages, discard the rest without summarization. Whether implemented as /clear --keep=N or /prune --keep-last N is an API design choice.

See also #7919 for a related request about preserving recent messages during compaction.

github-actions[bot] · 9 months ago

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.