# Session Cleanup

Identify stale and empty Claude Code sessions in the Agent Monitor and explain the...

- Repository: https://github.com/hoangsonww/Claude-Code-Agent-Monitor (1136 stars)
- Category: Data and ML
- License: MIT
- Last updated: 2026-10-31
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add hoangsonww/Claude-Code-Agent-Monitor --skill session-cleanup -g -a claude-code -y`
Codex: `npx skills add hoangsonww/Claude-Code-Agent-Monitor --skill session-cleanup -g -a codex -y`
Cursor: `npx skills add hoangsonww/Claude-Code-Agent-Monitor --skill session-cleanup -g -a cursor -y`
Gemini CLI: `npx skills add hoangsonww/Claude-Code-Agent-Monitor --skill session-cleanup -g -a gemini-cli -y`
Claude Code plugin marketplace: `/plugin marketplace add hoangsonww/Claude-Code-Agent-Monitor`

## 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.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# 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.

The rest: https://github.com/hoangsonww/Claude-Code-Agent-Monitor/blob/HEAD/plugins/ccam-sessions/skills/session-cleanup/SKILL.md
