memory-gc

A command for cleaning obsolete entries from a coding agent's stored memory, with the option to archive information that may still have historical value.

In plain words
What is it for?
Use it to inspect memory entries, find items older than the configured threshold, remove entries that are no longer true, and archive useful history.
Why use it?
It prevents old, incorrect, or superseded notes from being treated as current information and identifies missing or stale memory data.

Command

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/stefan-jansen/claude-code-toolkit/memory-gc
Clone the repo
git clone --depth 1 https://github.com/stefan-jansen/claude-code-toolkit
Per session 14 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,423 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00014 $0.01423
Opus 5 $0.00007 $0.00711
Sonnet 5 $0.00003 $0.00285
Haiku 4.5 $0.00001 $0.00142

Measured yesterday against content hash e148236824d0, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

memory-gc scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

plugins/memory/commands/memory-gc.md · 198 lines

How it starts

The opening of the file, as written. The whole thing — 198 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Memory Garbage Collection

Systematic cleanup of stale, obsolete, or incorrect memory entries.

Philosophy: Removal Without Guilt

Memory should reflect CURRENT reality, not history. Remove entries proven wrong, superseded, or obsolete. Archive if historical value exists.

#!/bin/bash

# Constants
MEMORY_DIR=".claude/memory"
ARCHIVE_DIR=".claude/work/archives/memory"
CURRENT_DATE=$(date +%Y-%m-%d)
STALENESS_THRESHOLD=30

echo "Memory Garbage Collection - $CURRENT_DATE"
echo ""

# Check if memory directory exists
if [[ ! -d "$MEMORY_DIR" ]]; then
    echo "❌ No memory directory found at $MEMORY_DIR"
    exit 1
fi

# Function to calculate days since date
days_since() {
    local date_str=$1
    if [[ -z "$date_str" ]] || [[ "$date_str" == "N/A" ]]; then
        echo "999"
        return
    fi
    local date_epoch=$(date -d "$date_str" +%s 2>/dev/null || date -j -f "%Y-%m-%d" "$date_str" "+%s" 2>/dev/null || echo 0)
    local current_epoch=$(date +%s)
    local diff_days=$(( (current_epoch - date_epoch) / 86400 ))
    echo $diff_days
}

# Step 1: Identify stale files
echo "📋 Step 1: Identify Stale Files"
echo "--------------------------------"
echo ""

stale_files=()

for file in "$MEMORY_DIR"/*.md; do
    if [[ ! -f "$file" ]]; then
        continue
    fi

    filename=$(basename "$file")
    last_validated=$(grep -oP "Last (validated|updated).*?(\d{4}-\d{2}-\d{2})" "$file" | tail -1 | grep -oP "\d{4}-\d{2}-\d{2}" || echo "")

    if [[ -z "$last_validated" ]]; then
        echo "⚠️  $filename - No timestamp found"
        stale_files+=("$filename")
    else
        days=$(days_since "$last_validated")
        if [[ $days -gt $STALENESS_THRESHOLD ]]; then
            echo "🔴 $filename - Stale ($days days since validation)"
            stale_files+=("$filename")
        else
            echo "✅ $filename - Fresh ($days days)"
        fi
    fi
done

echo ""

# Step 2: Review stale entries
if [[ ${#stale_files[@]} -eq 0 ]]; then
    echo "✅ No stale files found!"
    echo "   All memory entries are fresh (<$STALENESS_THRESHOLD days)"
    echo ""
    exit 0
fi

echo "📝 Step 2: Review Stale Content"
echo "--------------------------------"
echo ""
echo "Found ${#stale_files[@]} stale file(s) to review"
echo ""

# Interactive review
for filename in "${stale_files[@]}"; do
    file="$MEMORY_DIR/$filename"

    echo "Reviewing: $filename"
    echo "---"
    echo ""

    # Show file summary
    echo "First 20 lines:"
    head -20 "$file"
    echo ""
    echo "[... file continues ...]"
    echo ""

    # Ask what to do
    echo "Actions:"
    echo "  1) Keep and update timestamp (content still valid)"
    echo "  2) Archive (historical value but not current)"
    echo "  3) Delete (incorrect or obsolete)"
    echo "  4) Skip (review later)"
    echo ""
    read -p "Choice [1-4]: " choice

    case $choice in
        1)
            # Update timestamp
            if grep -q "Last validated:" "$file"; then
                sed -i "s/Last validated:.*$/Last validated: $CURRENT_DATE/" "$file"
            elif grep -q "Last updated:" "$file"; then
                sed -i "s/Last updated:.*$/Last updated: $CURRENT_DATE/" "$file"
            else
                sed -i "2i\\**Last validated**: $CURRENT_DATE\\n" "$file"
            fi
            echo "✅ Timestamp updated"
            echo ""
            ;;

        2)
            # Archive
            mkdir -p "$ARCHIVE_DIR"
            archive_name="${filename%.md}_${CURRENT_DATE}.md"
            mv "$file" "$ARCHIVE_DIR/$archive_name"
            echo "📦 Archived to $ARCHIVE_DIR/$archive_name"
            echo ""
            ;;

        3)
            # Delete
            read -p "⚠️  Confirm deletion of $filename [y/N]: " confirm
            if [[ "$confirm" == "y" ]]; then
                rm "$file"
                echo "🗑️  Deleted"
            else
                echo "⏭️  Skipped deletion"
            fi
            echo ""
            ;;

        4)
            echo "⏭️  Skipped"
            echo ""
            ;;

        *)
            echo "❌ Invalid choice, skipping"
            echo ""
            ;;
    esac
done

# Summary
echo "📈 Step 3: Garbage Collection Summary"
echo "--------------------------------------"
echo ""

remaining_files=$(find "$MEMORY_DIR" -name "*.md" -type f | wc -l)
total_size=$(du -sh "$MEMORY_DIR" 2>/dev/null | cut -f1)

echo "Memory state after GC:"
echo "  - Files remaining: $remaining_files"
echo "  - Total size: $total_size"
echo ""

if [[ -d "$ARCHIVE_DIR" ]]; then
    archived_count=$(find "$ARCHIVE_DIR" -name "*.md" -type f 2>/dev/null | wc -l)
    echo "  - Archived entries: $archived_count"
    echo ""
fi

echo "✅ Garbage collection complete"
echo ""
echo "💡 Next steps:"
echo "   - Run /memory-review to verify state"
echo "   - Run /memory-update to add new learnings"
echo "   - Schedule next GC in ~30 days"

Read the full file on GitHub · 198 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 198 lines · 14 tokens per session scan A e148236824d0

Subscribe to this mod's changes

memory-gc is a command published in the GitHub repository stefan-jansen/claude-code-toolkit (85 stars, last pushed 1mo ago), licensed MIT. It adds 14 tokens to every session and 1,423 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.