Openclaw Session Cleaner

OpenClaw Session Cleaner: Manage, compress, and rebuild your sessions efficiently

What Is Openclaw Session Cleaner?

Openclaw Session Cleaner is a utility designed to help developers and system administrators manage and maintain OpenClaw session files. OpenClaw, a popular automation and agent framework, heavily relies on session management for tracking user and cron-triggered automation states. Over time, these session files—especially those generated by scheduled cron jobs—can accumulate and lead to file bloat, performance degradation, and even functional errors due to oversized metadata like sessions.json. Openclaw Session Cleaner provides a systematic, scriptable approach to cleaning up redundant session files, compressing or rebuilding the sessions.json index, and ensuring that only valid, active session references are retained.

This tool operates by scanning the default OpenClaw session directory (~/.openclaw/agents/main/sessions/), assessing the quantity and size of session artifacts, and then executing targeted cleanup routines as directed. Importantly, it safeguards core session files, such as the main session, while efficiently removing outdated or orphaned cron session logs.

Why Use Openclaw Session Cleaner?

As OpenClaw deployments mature, it is common for thousands of session files to accumulate, particularly when cron jobs are used for scheduled tasks. This leads to several challenges:

  • Disk space consumption: Excessive .jsonl session files and an ever-growing sessions.json index file can consume significant storage.
  • Performance issues: Large session directories and bloated index files slow down OpenClaw’s startup and session resolution routines.
  • Maintenance overhead: Manual cleanup is error-prone and often results in accidental deletion of important sessions or incomplete reference removal.
  • Corrupted or out-of-sync indices: If sessions.json references files no longer present on disk, inconsistencies and errors can arise.

Openclaw Session Cleaner automates the identification and removal of obsolete session files, ensuring the system remains performant and maintainable. For teams running long-lived agents or frequent automation, this tool becomes essential for operational hygiene.

How to Get Started

To use Openclaw Session Cleaner, you will need access to your OpenClaw installation and the session storage directory. The tool is available as an open-source skill at GitHub.

Installation

Clone the skills repository or download the relevant script directly:

git clone https://github.com/chujianyun/skills.git
cd skills/skills/openclaw-session-cleaner

Ensure you have Python 3.6+ installed. Most users can run the cleaner as a script or integrate it into their workflow via OpenClaw’s skill invocation mechanisms.

Basic Usage

To execute a session cleanup, either invoke the skill through the OpenClaw agent interface (if integrated) or run the script directly:

python openclaw_session_cleaner.py

The script will:

  1. Inspect ~/.openclaw/agents/main/sessions/ for session files.
  2. Analyze the size of sessions.json.
  3. Identify and remove outdated cron session files, preserving the main session.
  4. Rebuild sessions.json to reflect only present session files.
  5. Report before-and-after statistics to the user.

Example:

Manual Cleanup

Suppose you want to manually clear old cron sessions and rebuild the index. You might use a Python snippet like the following to identify and remove extraneous .jsonl files:

import os
import json

SESSIONS_DIR = os.path.expanduser('~/.openclaw/agents/main/sessions/')
SESSIONS_INDEX = os.path.join(SESSIONS_DIR, 'sessions.json')

## Load current index
with open(SESSIONS_INDEX, 'r') as f:
    sessions = json.load(f)

## Identify main session (not to be deleted)
main_session = 'main_session.jsonl'
session_files = [f for f in os.listdir(SESSIONS_DIR) if f.endswith('.jsonl')]

## Delete old cron sessions
for file in session_files:
    if file != main_session and file.startswith('cron_'):
        os.remove(os.path.join(SESSIONS_DIR, file))

## Rebuild sessions.json index
valid_sessions = [f for f in os.listdir(SESSIONS_DIR) if f.endswith('.jsonl')]
with open(SESSIONS_INDEX, 'w') as f:
    json.dump(valid_sessions, f, indent=2)

Key Features

  • Automatic Detection: Scans and tallies session files and the size of sessions.json before performing actions.
  • Selective Purging: Identifies redundant cron-generated .jsonl files for removal while protecting critical sessions.
  • Index Rebuilding: Ensures sessions.json only references session files actually present on disk, eliminating stale references.
  • Reporting: Outputs pre- and post-cleanup statistics, including file counts and index size, providing clear feedback on the cleanup’s impact.
  • Safe Defaults: Always preserves the main session and avoids aggressive deletions unless explicitly instructed.

Best Practices

  • Schedule Regular Cleanups: Integrate Openclaw Session Cleaner into your maintenance routines, especially if you use cron jobs extensively.
  • Backup Before Cleaning: Always make a backup of your session directory and sessions.json before running a cleanup.
  • Monitor Growth: Use the tool’s reporting features to monitor session file growth over time and adjust cron job retention policies as needed.
  • Automate with Caution: If automating the skill, ensure you have logic to avoid deleting sessions in active use.

Important Notes

  • Default Target Path: The tool operates on ~/.openclaw/agents/main/sessions/ by default. Adjust the path if your deployment uses a different directory.
  • Session Identification: OpenClaw’s internal logic determines which sessions are safe to remove. Manual overrides should be used with care.
  • Main Session Safety: The main session file is always preserved. Verify which file is considered the main session in your deployment.
  • Consistency Assurance: After cleanup, sessions.json will only reference files still present on disk, reducing the risk of index-related errors.
  • Permissions: Ensure the user running the cleaner has sufficient filesystem permissions to delete files and modify sessions.json.
  • Reporting: After execution, review the tool’s report to verify the intended changes were made and no critical data was lost.

By following these guidelines and leveraging Openclaw Session Cleaner, OpenClaw users can maintain a healthy, performant session management environment with minimal manual intervention.