[FEATURE] Pre-clear hook: access conversation history before /clear wipes context
Preflight Checklist
- [x] I have searched existing requests and this feature hasn't been requested yet
- [x] This is a single feature request (not multiple features)
Problem Statement
What I want to do
Run a skill or script at the end of a session that summarises what
happened — before /clear removes the conversation history. The
summary depends on that history, so any hook that fires after the
clear is too late.
Current behaviour
SessionStart with matcher: "clear" fires after the context is wiped.
SessionEnd is documented to fire on /clear but does not (see
#6428). Either way, by the time any hook runs, the conversation
history is gone.
Requested behaviour
A hook event that fires before /clear executes, while conversation
history is still accessible. This would allow skills, scripts, or
agents to read and act on the session before it is cleared.
Something like:
{
"hooks": {
"PreClear": [
{
"type": "command",
"command": "sh ~/.claude/hooks/session-save.sh"
}
]
}
}
Related requests
This is part of a broader pattern of hooks firing too late to be
useful:
- #16659 — requests a clearContext field on the Stop hook to detect
when a clear is about to happen
- #21190 — requests a pre-compact hook to preserve conversation
history before compaction wipes it (same problem, different trigger)
- #12755 — requests a blocking pre-exit hook, with the same
argument: current hooks fire after the state is already gone
Use case
Automated session logging for project memory. I have a skill (/session-save) that
reads the conversation, writes a summary, and saves it to a file. It
works perfectly when run manually before /clear. A pre-clear hook
would let this run automatically.
Proposed Solution
Add a PreClear hook event that fires synchronously before /clear
executes. Claude Code should:
- Fire the PreClear hook
- Wait for it to complete
- Then wipe the conversation history
This makes it blocking by default — the same pattern requested in
#12755 for pre-exit. A non-blocking option could be added later, but
for session logging the hook needs to complete before the history
disappears.
For consistency with the existing SessionStart matcher pattern, an
alternative would be extending SessionStart with a pre-clear matcher
that fires before the clear rather than after:
{
"hooks": {
"SessionStart": [
{
"matcher": "pre-clear",
"hooks": [
{
"type": "command",
"command": "sh ~/.claude/hooks/session-save.sh"
}
]
}
]
}
}
Either approach would work. The key requirement is that conversation
history is still intact when the hook runs.
Alternative Solutions
_No response_
Priority
Medium - Would be very helpful
Feature Category
Configuration and settings
Use Case Example
_No response_
Additional Context
I built a session-logging skill that summarises what happened during a Claude Code session and saves it to a file. It works perfectly when run manually before /clear. Without a pre-clear hook, it can't run automatically — the conversation history it needs to generate the summary is already gone by the time any current hook fires.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗