Feature request: PreClear hook event (fires before /clear wipes context)
Feature Request: PreClear Hook Event
Problem
There is no hook event that fires before /clear executes. The closest option is SessionEnd with matcher: "clear", but it fires after the context is already wiped — making it impossible to perform AI-driven cleanup that depends on conversation context.
Our Workflow
We use Claude Code's hooks system to maintain a growth feedback system — a per-user file that tracks observations about work patterns, technical decisions, and collaboration style across sessions. These observations are written by the AI during the conversation and stored in .claude/growth-notes/{user}.md.
Our current hook coverage:
| Event | Hook | What it does |
|-------|------|-------------|
| PreCompact | pre-compact-growth.sh | Appends a compaction timestamp marker before context is compressed. Works great — context is still available. |
| SessionEnd (clear) | session-end-growth.sh | Appends a "session cleared" marker after /clear. Context is already gone — can only write a timestamp, not a meaningful summary. |
| /wr-bye (custom command) | AI-driven | Full growth summary written by the AI before the user exits. Requires the AI to still have conversation context. |
The gap: when a user types /clear instead of our custom /wr-bye, any unwritten growth observations from the current conversation are lost. The AI had context to write them, but there's no hook that fires while that context still exists.
Proposed Solution
Add a PreClear hook event that fires before /clear wipes the conversation context, analogous to how PreCompact fires before compaction.
{
"hooks": {
"PreClear": [
{
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/scripts/pre-clear-growth.sh",
"timeout": 10
}
]
}
]
}
}
Even better: if a PreClear hook could inject a system message that the AI processes before the clear (similar to how UserPromptSubmit can inject reminders), the AI could write final observations to disk before context is destroyed.
Why This Matters
The PreCompact / PreClear symmetry is intuitive:
PreCompact= "context is about to be compressed, save what you need" ✅ existsPreClear= "context is about to be destroyed, save what you need" ❌ missing
Any workflow that maintains state derived from conversation context (summaries, notes, logs, checkpoints) benefits from a chance to flush that state before /clear destroys it.
Alternatives Considered
SessionEndwith matcher"clear"— fires too late, context is gone- Writing observations continuously during the session — we do this, but there's always a gap between the last observation and
/clear - Training users to use
/wr-byeinstead of/clear— works but fragile; people forget
Environment
- Claude Code CLI
- macOS
- Using hooks extensively:
SessionStart,PreToolUse,UserPromptSubmit,PreCompact,SessionEnd
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗