Skip to main content
skill CX Codex

session-cleanup — Claude Code skill: prune stale agent sessions

Finds empty and abandoned sessions in Agent Monitor, lists what would go, and deletes only after you confirm.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add hoangsonww/Claude-Code-Agent-Monitor --skill session-cleanup -a claude-code
Open on GitHub

What session-cleanup does

The skill reads the monitoring database through GET /api/stats and GET /api/sessions?limit=1000. It flags sessions with no events, a turn_count of 0 or null and zero cost, plus rows still marked active or working whose last activity is older than the threshold. That threshold comes from the argument you pass; 24 hours is the default.

You get a preview table first: short id, status, the reason it qualified, cwd basename, start time, cost in USD to four decimals. Nothing is removed at this stage. The actual deletion runs through POST /api/settings/cleanup, a server-side routine that drops empty and orphaned sessions along with their dangling events.

There is no undo from the dashboard, which is why the skill waits for an explicit confirm before touching anything. It is the only skill in the plugin that mutates data, and it stays on that one documented endpoint rather than reaching for POST /api/settings/clear-data. If the dashboard is down, start it with npm start from the repo root.

What it helps with

  • spot sessions that started and never did any work
  • tell abandoned active sessions from ones still running
  • review the deletion list before any data is gone
  • tidy the monitoring database after a batch of dropped runs
  • compare stats before and after a cleanup

Example prompts for the agent

  • List the Agent Monitor sessions that can be removed, don't delete anything
  • Check sessions older than 7d and show me the candidate table
  • Confirmed — run the cleanup and show the stats delta

How to install session-cleanup

1Skills CLI (Vercel)

npx skills add hoangsonww/Claude-Code-Agent-Monitor --skill session-cleanup -a claude-code

2Manually: put the files in place

~/.claude/skills/session-cleanup/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/session-cleanup/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about session-cleanup

How do I install session-cleanup in Claude Code?

Run npx skills add hoangsonww/Claude-Code-Agent-Monitor --skill session-cleanup -a claude-code. The skill lands at ~/.claude/skills/session-cleanup/SKILL.md.

What does session-cleanup do?

It pulls monitoring stats, marks empty, stale and truncated sessions, prints them as a table, and deletes them only once you confirm.

Can a cleanup be undone?

No. POST /api/settings/cleanup removes rows permanently and the dashboard offers no rollback, so the skill previews by default.

Source text

SKILL.md · MIT
---
name: session-cleanup
description: >
  Identify stale and empty Claude Code sessions in the Agent Monitor and explain the
  cleanup endpoint (POST /api/settings/cleanup), always showing the exact list of
  what WOULD be removed before anything is deleted. Cleanup permanently deletes data,
  so this skill previews first and requires explicit user confirmation. Use when
  tidying the monitoring database.
---

# Session Cleanup

Find prune-worthy sessions and explain cleanup — preview first, delete only on
explicit confirmation.

## Input

The user provides: **$ARGUMENTS**

- Empty / `preview` → only show what would be removed (the safe default).
- `confirm` → the user has reviewed the preview and explicitly authorizes deletion.
- An optional staleness threshold (e.g. `older than 7d`) for what counts as stale.

## Data Sources

| Endpoint | Returns |
|----------|---------|
| `GET /api/sessions?limit=N` | session list: id, status, model, cwd, started_at, ended_at, cost, metadata (turn_count, total_turn_duration_ms) |
| `GET /api/stats` | totals: total_sessions, active_sessions, total_events, events_today, sessions_by_status, agents_by_status |
| `POST /api/settings/cleanup` | runs the cleanup routine and returns what was removed — DESTRUCTIVE, only after confirmation |

## Report Sections

### 1. Baseline
`GET /api/stats` — record total_sessions, sessions_by_status, total_events. This is
the before-state to compare against.

### 2. Identify candidates
`GET /api/sessions?limit=1000`. Flag sessions that are:
- **Empty** — zero events and `turn_count` 0 / null and `cost` 0 (started but never
  did anything).
- **Stale active** — `status` active/working but last activity older than the
  threshold (default 24h), i.e. never cleanly stopped.
- **Truncated** — no `ended_at` and no recent events.

### 3. Preview table (ALWAYS shown)
List every candidate with the reason it qualifies. State the total count and confirm
that **nothing has been deleted yet**.

### 4. Explain the endpoint
Describe `POST /api/settings/cleanup`: it prunes empty / orphaned sessions and their
dangling events server-side and returns a summary of removed rows. Make clear this
is **permanent** and **not reversible** from the dashboard.

### 5. Execute only on confirmation
If — and only if — `$ARGUMENTS` is `confirm` (or the user has explicitly approved
this run), call `POST /api/settings/cleanup`, then re-read `/api/stats` and report
the before → after delta. Otherwise stop after the preview and tell the user to
re-run with `confirm`.

## Output

A preview Markdown table: `id (short) | status | reason | cwd basename | started_at | cost`,
then a one-line count and the explicit "nothing deleted — re-run with `confirm` to
proceed" notice. On a confirmed run, add a before → after summary using ▲/▼ on the
counts. Currency as USD to 4 decimal places.

## Safety

- This is the ONLY skill in the plugin that mutates data, and only via the one
  documented endpoint.
- NEVER call `POST /api/settings/cleanup` without an explicit `confirm` from the
  user in this turn — previewing is the default.
- Never widen scope to `POST /api/settings/clear-data` or any other destructive
  endpoint; cleanup of stale/empty sessions only.
- If the dashboard is unreachable, tell the user to start it with `npm start` from
  the repo root.

© hoangsonww · MIT · hoangsonww/Claude-Code-Agent-Monitor

Similar skills

All skills for Agent setup: memory, context, orchestration →

From the same set20

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor