Nightly maintenance for OpenClaw — memory organization (sort loose memory files into topic folders with frontmatter and INDEX.md) and session cleanup (purge...
--- name: openclaw-maintenance description: Nightly maintenance for OpenClaw — memory organization (sort loose memory files into topic folders with frontmatter and INDEX.md) and session cleanup (purge tombstones, old cron sessions, orphans across all agents). Use when memory files are disorganized, session folders are bloated, disk space is low, or to set up automated daily maintenance. Triggers on "clean sessions", "organize memory", "maintenance", "session bloat", "memory cleanup", "free disk space", "nightly cleanup". --- # OpenClaw Maintenance Two scripts that keep your OpenClaw instance lean: memory organization and session cleanup. ## 1. Memory Organizer Sorts loose `.md` files from `~/.openclaw/workspace/memory/` root into topic subfolders. ### What it does - Scans `memory/*.md` for files sitting in the root (not already in a subfolder) - Routes each file to a topic folder based on keyword matching: - `cabinet/` — agent, cron, delegation, cabinet agent names - `content/` — post, blog, marketing, seo, clips - `products/` — copanion, hypercho, feature, ui, roadmap - `technical/` — bug, error, config, docs, gateway - `x/` — twitter, viral, engagement, followers - `user/` — ziwen, founder, personal - `daily/` — fallback for everything else - Adds YAML frontmatter (topic, date, tags) if missing - Updates `INDEX.md` in each topic folder with the new file - Idempotent — safe to run repeatedly - No LLM dependency — pure keyword matching ### Run ```bash python3 <skill_dir>/scripts/memory_organize.py ``` ## 2. Session Cleanup Cleans session storage across ALL registered agents. ### What it cleans | Target | Retention | Action | |--------|-----------|--------| | Tombstones (`.reset.*`, `.deleted.*`, `.bak-*`) | 0 days | Always delete | | Cron session `.jsonl` files | 7 days | Delete after 7 days | | Orphan `.jsonl` (on disk, not in sessions.json) | 0 days | Delete | | Stale sessions.json entries (cron, file missing) | 0 days | Remove entry | | Non-cron sessions | 30 days | Keep | | Main sessions | Forever | Never touched | | Active sessions (`.lock`) | Forever | Never touched | ### Safety - Auto-discovers agents by scanning `~/.openclaw/agents/*/sessions/` - Backs up `sessions.json` before modifying - Never touches locked/active sessions ### Run ```bash # All agents python3 <skill_dir>/scripts/session_cleanup.py # Preview only python3 <skill_dir>/scripts/session_cleanup.py --dry-run # Single agent python3 <skill_dir>/scripts/session_cleanup.py --agent main ``` ## Cron Setup Set up a single midnight cron that runs both scripts: ``` Schedule: 0 0 * * * (midnight local time) Model: any cheap/fast model Thinking: low Timeout: 600s Delivery: none ``` Cron task message: ``` Single task only: run memory organizer and session cleanup. Command 1 (memory): bash -lc 'python3 <skill_dir>/scripts/memory_organize.py' Command 2 (sessions): bash -lc 'python3 <skill_dir>/scripts/session_cleanup.py' Return ONLY the combined stdout (no extra commentary). ```
don't have the plugin yet? install it then click "run inline in claude" again.
added explicit intent, inputs (env vars, file paths, no external apis), detailed procedure steps with inputs/outputs for both scripts, decision points for idempotency and edge cases (rate limits, permission errors, stale backups, dry-run mode), output contract specifying data formats and file locations, and outcome signals for user verification.
Two scripts that keep your OpenClaw instance lean: memory organization and session cleanup. Run nightly via cron or manually when memory files pile up in root, session folders bloat, or disk space runs low.
this skill maintains openclaw's memory and session storage by running two complementary scripts. the memory organizer sorts loose markdown files from ~/.openclaw/workspace/memory/ root into topic subfolders based on keyword matching, adds yaml frontmatter, and updates INDEX.md in each folder. the session cleanup script purges tombstones, old cron sessions, orphan files, and stale entries across all registered agents while preserving active and main sessions. use this skill when memory files are disorganized, session folders are bloated, disk space is low, or to set up automated daily maintenance. triggers: "clean sessions", "organize memory", "maintenance", "session bloat", "memory cleanup", "free disk space", "nightly cleanup".
local environment
OPENCLAW_HOME: path to openclaw root directory (defaults to ~/.openclaw)OPENCLAW_WORKSPACE: path to workspace root (defaults to ~/.openclaw/workspace)$OPENCLAW_WORKSPACE/memory/$OPENCLAW_HOME/agents/*/sessions/python runtime
os, json, pathlib, datetime, re, shutilfile system access
memory/ directory and all topic subfolderssessions/ directories and sessions.json filessessions.jsonno external api connections required , this is pure local file maintenance.
scan root memory directory
$OPENCLAW_WORKSPACE/memory/.md files in the root (not in subfolders)categorize each file by keyword matching
.md file, read the filename and first 500 chars of contentcabinet/ , keywords: agent, cron, delegation, cabinet agent namescontent/ , keywords: post, blog, marketing, seo, clipsproducts/ , keywords: copanion, hypercho, feature, ui, roadmaptechnical/ , keywords: bug, error, config, docs, gatewayx/ , keywords: twitter, viral, engagement, followersuser/ , keywords: ziwen, founder, personaldaily/ , fallback (catch-all)add yaml frontmatter if missing
--- yaml blocktopic: (from categorization step 2)date: (file modification date, or today if unavailable)tags: (empty list, or infer from keywords if desired)move file to topic folder
memory/<topic>/memory/<filename>.md to memory/<topic>/<filename>.mdupdate INDEX.md in topic folder
memory/<topic>/INDEX.md exists- [[<filename>]] with optional date/tagsdiscover all agents
$OPENCLAW_HOME/agents/sessions/ folder existsback up sessions.json
sessions.json filesessions.json.bak-<timestamp>list all session files on disk
sessions/ folder for the agent.jsonl files and special files (tombstones: .reset.*, .deleted.*, .bak-*)read sessions.json (active session registry)
sessions.json for the agentapply retention policy to tombstones and orphans
.reset.*, .deleted.*, .bak-*): delete immediately (0 day retention).jsonl file not in sessions.json (orphan): delete immediatelyapply retention policy to cron sessions
type: "cron":.jsonl file on disk if it existsapply retention policy to other sessions
.lock file present): never deletevalidate sessions.json after cleanup
write sessions.json back
--dry-run flag: do not write, only report what would be deletedif file already in correct topic subfolder
if frontmatter exists in memory file
if memory file matches multiple keyword categories
if INDEX.md entry for file already exists
if sessions.json does not exist for an agent
if --dry-run flag is set
if --agent
if session file has .lock present
if backup sessions.json.bak-
if file system operation fails (permission denied, disk full, etc.)
memory organizer outputs
.md file moved from memory/ root to memory/<topic>/memory/<topic>/INDEX.md created or updated with file entriessession cleanup outputs
sessions/ directories.jsonl files deletedsessions.json.bak-<timestamp> backup created before any modificationsif --dry-run
memory organizer success
.md files no longer sit in memory/ root (all moved to topic subfolders)session cleanup success
both scripts via cron