libra clean
Command reference for `libra clean`
Remove untracked files (and optionally directories) from the working tree.
Synopsis
libra clean -n [-d] [-x | -X] [-e <pattern> | --exclude <pattern>]... [--json] [--quiet] [pathspec]...
libra clean -f [-d] [-x | -X] [-e <pattern> | --exclude <pattern>]... [--json] [--quiet] [pathspec]...
Description
libra clean removes untracked files from the working tree. Unlike Git,
Libra requires an explicit mode flag: -n for a dry-run preview or -f
for actual deletion. Running libra clean without either flag is an
error. This prevents accidental data loss by forcing the user to state
intent explicitly.
By default, only files are removed and the shared Git/Libra ignore sources
are honored (ignored files are skipped): .gitignore, the worktree-local
info/exclude (.libra/info/exclude, plus .git/info/exclude in a
Git- or dual-layout tree), core.excludesFile, and .libraignore — nearest directory
wins, and a .libraignore beats a sibling .gitignore; see
check-ignore.md for the full precedence. The -d flag opts into removing untracked
directories as well; -x opts into removing files the ignore rules would
otherwise protect; -X flips the rules so that only ignored files are
removed. Every candidate path is canonicalized and verified to reside
inside the worktree root before deletion, preventing symlink-escape
attacks.
Optional pathspecs limit cleaning through the shared pathspec engine — the
same matcher ls-files/status and the write commands (add/rm/restore)
use: glob patterns, :(exclude), :(top), :(icase), :(literal),
:(glob), and subdirectory-relative semantics. Deletion stays safe by
construction: candidates are always untracked-only, exclusion magic can only
narrow the set, the -n preview set is exactly the -f deletion set, and an
empty-string pathspec is rejected rather than widening to the whole tree.
A nested repository — a subdirectory containing its own .git or .libra
entry — is protected under a single -f: its files are never candidates, an
ancestor directory that contains it is not removed, and -n never lists it. The same protection applies to an explicit pathspec: clean -f foo/, clean -f foo/file and clean -f parent/ (where parent/ contains a nested repository) keep every nested file.
This matches Git's default REMOVE_DIR_KEEP_NESTED_GIT, which keeps nested
worktrees when one force is given.
Options
| Flag | Short | Long | Description |
|---|---|---|---|
| Dry run | -n |
--dry-run |
Show what would be removed without deleting anything. |
| Force | -f |
--force |
Actually remove untracked files. |
| Directories | -d |
--dir |
Also remove untracked directories (otherwise only files). |
| Include ignored | -x |
Remove untracked files including those matched by ignore rules. | |
| Only ignored | -X |
Remove only untracked files that are matched by ignore rules. | |
| Exclude | -e |
--exclude <pattern> |
Add an extra exclusion pattern; may be repeated. |
| Pathspec | positional | Limit candidates through the shared pathspec engine (glob, :(exclude), :(top), :(icase), :(literal), :(glob), subdirectory-relative). Exclusions only narrow the deletion set. |
|
| JSON | --json |
Emit structured JSON output (see below). | |
| Quiet | --quiet |
Suppress all human-readable stdout. |
-x and -X are mutually exclusive — -x includes ignored files in
addition to normally-untracked ones, -X restricts the operation to
ignored files only.
Option Details
-n / --dry-run
Preview mode. Lists every untracked path that would be deleted without touching the filesystem. No objects are written and no operation is recorded:
$ libra clean -n
Would remove build/output.log
Would remove notes.txt
-f / --force
Deletion mode. Removes every untracked path and reports each removal:
$ libra clean -f
Removing build/output.log
Removing notes.txt
-d / --dir
Opt-in for untracked directories. Without -d, untracked directories
are left in place (their contents are still considered if the directory
itself is tracked). With -d, the directory tree is walked and the
empty directory is removed after its files are.
-x
Override configured ignore sources. Without this flag, ignored files (build
artifacts, caches, etc.) are skipped. With -x, they are treated like
any other untracked file and removed.
One exception, under every mode (-x and -X included): files materialized
by an active libra layer overlay are never removed. They are untracked by
construction, so clean would otherwise be free to delete them — and nothing
but libra layer apply could bring them back, because their content lives
outside the repository. A directory is likewise kept when an overlay file
exists anywhere beneath it, since -d removes whole trees. libra layer unapply is the way to remove overlay files. If clean cannot determine which
paths an overlay owns (a locked or unreadable layer store) it REFUSES rather
than deleting.
-X
Inverse of -x. Removes only the files that ignore sources would
normally protect. Useful for "clean my build artifacts but leave
hand-edited files alone."
-e / --exclude <pattern>
Add an additional exclusion pattern (in Git ignore syntax) for this invocation only. Can be passed multiple times to layer patterns:
libra clean -f --exclude '*.log' --exclude 'tmp/**'
Combining -n and -f: When both flags are passed, the dry-run
takes precedence and no files are deleted.
Common Commands
# Preview what would be removed
libra clean -n
# Remove all untracked files (files only)
libra clean -f
# Also remove untracked directories
libra clean -fd
# Remove untracked files including ignored ones (build artifacts, caches)
libra clean -fx
# Remove only ignored files (keep hand-edited files intact)
libra clean -fX
# Layer an additional exclusion pattern on top of configured ignore sources
libra clean -f --exclude '*.log'
# Preview in JSON format (useful for scripting)
libra clean -n --json
Human Output
Dry-run:
Would remove build/output.log
Would remove notes.txt
Forced removal:
Removing build/output.log
Removing notes.txt
--quiet suppresses stdout.
Structured Output (JSON)
{
"ok": true,
"command": "clean",
"data": {
"dry_run": true,
"removed": ["build/output.log", "notes.txt"]
}
}
removed is empty when there is nothing to clean.
Design Rationale
Why require an explicit mode flag?
Git's clean without -f (and without clean.requireForce = false)
prints an error asking for -f. This is a config-dependent guardrail.
Libra makes the guardrail unconditional: you must always pass -n or
-f. There is no configuration to weaken this requirement. This
eliminates an entire class of "I accidentally ran clean" incidents.
Why no interactive mode (-i)?
Git's interactive clean mode presents a menu for selecting files. Libra
targets AI-agent and scripting workflows where interactive prompts are
unusable. The dry-run/force two-step workflow achieves the same safety
with full automation support: run -n --json to inspect, then -f to
execute.
Why ship -d / -x / -X after originally declining them?
The original clean design intentionally declined the directory and
ignore-override flags out of safety concerns (docs/development/commands/clean.md
listed them as non-goals). Subsequent user feedback showed that build
workflows in agent-driven environments routinely need to clear ignored
artifacts, and the missing flags forced users to fall back on raw rm -rf which is strictly less safe than clean (no symlink-escape
verification, no dry-run preview). The flags were added with the same
worktree-confinement and symlink checks as the base mode, preserving the
safety guarantees while restoring parity with git clean.
Parameter Comparison: Libra vs Git vs jj
| Parameter | Libra | Git | jj |
|---|---|---|---|
| Dry run | -n / --dry-run |
-n / --dry-run |
N/A (no clean command) |
| Force delete | -f / --force |
-f / --force |
N/A |
| Remove directories | -d / --dir |
-d |
N/A |
| Ignore override (all) | -x |
-x |
N/A |
| Ignore override (only ignored) | -X |
-X |
N/A |
| Exclude pattern | -e <pattern> / --exclude <pattern> (repeatable) |
-e <pattern> (repeatable) |
N/A |
| Interactive mode | Not supported | -i |
N/A |
| Quiet mode | --quiet |
-q / --quiet |
N/A |
| JSON output | --json |
Not supported | N/A |
| Pathspec filter | Shared pathspec engine (glob, :(exclude), :(top), :(icase), :(literal), :(glob)) |
<pathspec>... |
N/A |
| Require force config | Always required | clean.requireForce (default true) |
N/A |
Note: jj does not have a clean command because its working-copy model
tracks all files automatically and untracked files are not a concept in
the jj data model.
Error Handling
| Scenario | StableErrorCode | Exit |
|---|---|---|
Missing -f / -n |
LBR-CLI-002 |
129 |
| Corrupted index or untracked scan failure | LBR-IO-001 |
128 |
| Path resolves outside the worktree | LBR-CONFLICT-002 |
128 |
| File deletion failed | LBR-IO-002 |
128 |