◉CLAUDE LABJP
●CC 2.1.330 — Claude Code 2.1.330 fixes the multi-minute freeze right after compacting a long conversation●SONNET 4.5 — Sonnet 4.5 retires from the Claude API on 11/30, 8 days left. The suggested replacement is claude-sonnet-5-5●MCP 401 — Users report MCP servers that return 401 time out silently instead of showing an error●NEW — New: Desktop creates a worktree for every session? Three places to check●OPUS 4.5 — The 11/24 date for Opus 4.5 is a not-sooner-than floor, not a retirement notice●SCHEMA — Same schema, different shape: compare structured output from the Agent SDK and the Messages API●CC 2.1.330 — Claude Code 2.1.330 fixes the multi-minute freeze right after compacting a long conversation●SONNET 4.5 — Sonnet 4.5 retires from the Claude API on 11/30, 8 days left. The suggested replacement is claude-sonnet-5-5●MCP 401 — Users report MCP servers that return 401 time out silently instead of showing an error●NEW — New: Desktop creates a worktree for every session? Three places to check●OPUS 4.5 — The 11/24 date for Opus 4.5 is a not-sooner-than floor, not a retirement notice●SCHEMA — Same schema, different shape: compare structured output from the Agent SDK and the Messages API
Articles/Claude Code
⟐ Claude Code/2026-06-18Advanced

Moving Cleanup and Logging into a SessionEnd Hook

How to use Claude Code's new post-session hook to automate temp-file cleanup and log writing after a session ends, with real examples from a pipeline that processes several repositories in sequence.

Claude Code291hooks22SessionEndautomation110CI/CD19

✦ Premium Article

A few times a month, my nightly publishing job would finish without removing the .next build artifact or appending its log entry — even though generation and the push itself ran fine every time. Only the final cleanup went missing. The cause was always the same: I had written cleanup as "the tail end of the main work," so whenever the run took a different branch, it never reached the last steps.

The June 18, 2026 update added a post-session hook (SessionEnd) to Claude Code — a hook that always runs after a session ends. It turned out to be exactly the structural guarantee I needed for the work I kept losing because it lived at the bottom of the main flow. As an indie developer running several sites in sequence, here is what I actually moved into it and where I tripped.

Why cleanup at the tail of the flow gets dropped

The body of a session branches on errors, early returns, and conditionals. Cleanup placed at the very end only runs when you take the happy path. What I wanted was the same protection a shell trap or a language-level finally gives you, but applied to a Claude Code session — and that is precisely the niche SessionEnd fills.

In my case, three things kept getting dropped: removing the temporary build output (.next), appending that day's update log, and checking free disk space for next time. None of them affect article quality, but left alone they fill the disk until the next session cannot even start.

The basic shape of a SessionEnd hook

You register hooks under hooks in settings.json, keyed by event name. SessionEnd fires right before the session closes.

{
  "hooks": {
    "SessionEnd": [
      {
        "matcher": "*",
        "hooks": [
          {
            "type": "command",
            "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/on-session-end.sh"
          }
        ]
      }
    ]
  }
}

One thing worth remembering: a SessionEnd hook's exit code and stdout do not change the outcome of a session that is already wrapping up. Unlike a Stop hook, it is not meant to block and resume work — it is for the "one reliable chore after the fact." I confused it with a Stop hook at first and tried to call further work back from inside the cleanup script, which failed. Treat SessionEnd as a point of no return and the design becomes clear.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦A working SessionEnd hook config and why its exit code does not change the session outcome
✦Concrete scripts for moving temp-file removal and log appends into the hook
✦A real failure from putting teardown in the hook, and how to avoid it
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

⟐ Claude Code2026-09-02
Two hooks that record every model switch and stop the ones you never agreed to
Build a PostModelSwitch hook that logs every model change and a PreModelSwitch hook that blocks unapproved switches during unattended runs. Complete scripts, measured timings, and the reason the two jobs must stay separate.
⟐ Claude Code2026-08-28
Don't let your verification script's full output flow back through a hook
The check was working the whole time. The conversation was what ran out of room. Measured side by side: 58 bytes from one scan of 833 files, 42KB from another. Which paths actually reach the model, and how to enforce a budget on them.
⟐ Claude Code2026-05-04
Build a Pipeline Where Docs Update Automatically Every Time Your Code Changes
Keep README, CHANGELOG and API docs current on every push without erasing what humans wrote, using fenced auto-docs blocks. Covers the bash -e trap that silently skips your error handling in GitHub Actions, stacked Unreleased sections, and truncated replies.