Skip to Content

Library API

Use purgeit's scanner, cleaner and rule engine from code.

Everything the CLI does is a plain async-iterator API with no UI dependency. ESM only, Node 22+, fully typed.

import { clean, createRuleSet, loadConfig, scan, scanAll } from 'purgeit';

const { config } = await loadConfig({ cwd: process.cwd() });
const rules = createRuleSet(config);
const roots = ['/Users/me/dev'];

// stream while the scan runs…
for await (const event of scan(roots, { rules, minAge: 7 * 86_400_000 })) {
  if (event.type === 'entry') console.log(event.entry.rule.name, event.entry.bytes, event.entry.path);
}

// …or collect
const { entries, summary } = await scanAll(roots, { rules });

// delete, re-verifying every path
const targets = entries
  .filter((entry) => !entry.protection)
  .map((entry) => ({ path: entry.path, bytes: entry.bytes, unchangedSince: entry.lastActive }));

for await (const event of clean(targets, { rules, roots, idleFor: 7 * 86_400_000 })) {
  if (event.type === 'refused') console.log('skipped', event.path, event.refusal.message);
}

scan(roots, options)

An async generator of events: found (a rule claimed a path), then entry (measured) or dropped (empty, too-small, too-recent, unreadable), and finally done with a summary. Stopping the iteration, or aborting signal, stops the work.

OptionMeaning
rulesrequired — a RuleSet from createRuleSet()
signalan AbortSignal
depthnever descend deeper than this
excludeglobs relative to each root
minBytes, minAgedrop smaller / more recently active (ms) artifacts
includeEmptykeep artifacts with no files
homethe home directory whose Library, .Trash and Applications are never entered

An Entry has path, root, project, rule, evidence, bytes, files, lastActive and protection. scanAll runs a scan to completion and sorts entries (deletable largest first, protected last).

clean(targets, options)

An async generator of start, deleted, refused (with a typed Refusal), failed, warning and done. A refusal is a decision, not an error; one refused or failed path never stops the rest.

OptionMeaning
rules, rootsrequired — every target must resolve strictly inside a root and still be claimed by a rule
idleForrefuse anything modified within this many ms (default off)
dryRunrun every check, delete nothing
concurrencydeletions in flight (default 4)
historyFileappend outcomes here; nothing is written without it
signalstop starting new deletions

A Target is { path, bytes?, unchangedSince? } — unchangedSince refuses the path if anything inside changed after that time.

Rules and config

RULE_CATALOG, createRuleSet(config), restrictRuleSet, onlySafeRules, loadConfig, ConfigSchema, defineConfig. Lower level: measure, findProtection, discoverRoots, readHistory, appendHistory, defaultHistoryFile. User-fixable failures throw PurgeitError.

On this page