nb-clean cleans Jupyter notebooks for version control. It removes cell metadata,
execution counts, outputs, and the notebook's language version. It can check
notebooks in CI, clean files in place, or run as a Git filter or pre-commit
hook.
Installation
Install a published release from PyPI with uv:
uv tool install nb-clean
For a single invocation:
uvx nb-clean check notebooks/
Wheels contain the native executable and require Python 3.10 or later for
installation. They are built for macOS and Linux on ARM64 and x86-64, and
Windows on x86-64. Linux wheels require glibc 2.17 or later. Use Cargo or binary
archives on other platforms.
Install a published release from crates.io with Cargo:
cargo install nb-clean --locked
Building requires Rust 1.88 or later.
Download binary archives from
GitHub releases for macOS
(Apple Silicon and Intel), Linux (ARM64 and x86-64), and Windows (x86-64).
Extract the archive and put nb-clean or nb-clean.exe on your PATH. Linux
archives are built on Ubuntu 22.04 and use glibc. Build from source for systems
incompatible with those binaries. Running nb-clean does not require a Python
installation. Git is needed only for filter configuration.
Directories are searched recursively for .ipynb files, skipping hidden files
and directories (such as .ipynb_checkpoints) and paths ignored by Git. Paths
supplied explicitly are always processed. Use --no-ignore to disregard Git
ignore rules, or to include hidden files and directories. Pass both
to search all directories without either restriction:
--hidden
nb-clean clean --no-ignore --hidden notebooks/
Overlapping inputs and symlink aliases are processed once. Directory symlinks
encountered during recursion are skipped; an explicitly supplied directory
symlink is traversed. Notebook file symlinks are followed, and cleaning updates
their targets while preserving the links.
check reports unwanted content and exits with status 1 if any notebooks are
dirty. Cells are numbered from zero. clean updates changed files while
preserving their permissions. Already-clean files are left untouched. If a file
fails, remaining files are still processed and the command returns an error
status.
With no paths, both commands read stdin. Cleaning writes the notebook to stdout:
Clean stdin is passed through unchanged. Changed notebooks are written with
sorted keys, one-space indentation, Unicode text, and a trailing newline,
following nbformat's JSON layout.
nb-clean supports nbformat 4. It checks the structure needed for cleaning, keeps
unknown fields, and does not upgrade notebooks or repair cell IDs. Empty source
means an empty string or an array whose strings are all empty. Whitespace-only
cells are retained.
Options
These directory search options work with check and clean:
Option
Effect
--no-ignore
Disregard Git ignore rules
--hidden
Include hidden files and directories
The flags are independent: --no-ignore still skips hidden paths, and
--hidden still respects Git ignore rules. Neither flag affects explicitly
supplied files or stdin.
These options work with check, clean, and add-filter:
Option
Effect
-e, --remove-empty-cells
Remove empty cells, or report them in checks
-M, --remove-all-notebook-metadata
Remove all notebook metadata
-m, --preserve-cell-metadata
Preserve all cell metadata except transient fields
--preserve-cell-metadata-field FIELD
Preserve a selected cell metadata field; repeat for more fields
By default, notebook metadata is retained except language_info.version and
transient fields. Preserving outputs with -o still clears output execution
counts unless -c is also supplied.
For example, preserve cell tags and slideshow settings:
Following nbformat, nb-clean always removes notebook metadata orig_nbformat,
orig_nbformat_minor, and signature, and cell metadata trusted.
Preservation options do not override this rule. Their presence makes a notebook
dirty.
-M conflicts with -n. Preserving all cell metadata conflicts with selecting
individual fields. Use nb-clean --help or nb-clean clean --help for help,
and nb-clean --version for the version.
Git filter
From a Git working tree:
nb-clean add-filter
# Or configure the filter with cleaning options:
nb-clean add-filter --preserve-cell-metadata-field tags
nb-clean remove-filter
The filter is configured in the local Git configuration and
.git/info/attributes. Staging cleans the notebook in the index; the working
copy keeps its outputs. Installation and removal are idempotent and preserve
unrelated settings and attributes. The executable must be on PATH when Git
stages notebooks.
Append cleaning options to the command array as needed.
Exit statuses
Status
Meaning
0
Success, or all checked notebooks are clean
1
At least one checked notebook is dirty
64
Invalid command-line arguments
65
Malformed notebook or unsupported format version
66
Input cannot be opened or a directory cannot be traversed
69
Git executable is unavailable
73
Output cannot be created or replaced
74
Other read or write failure
78
Git repository or configuration error
Errors take precedence over dirty results. If several files fail, the status
corresponds to the first error in sorted processing order. Errors go to stderr;
check findings go to stdout.
Migrating from nb-clean 5
check and clean now skip hidden files and directories, including
.ipynb_checkpoints, and Git-ignored paths when searching directories.
Previously, these notebooks were included.
To retain the version 5 directory search behaviour, add both --no-ignore and
--hidden:
The flags are independent: --no-ignore disables Git ignore rules, while
--hidden includes hidden files and directories. Explicitly supplied files are
still processed regardless of either restriction. Stdin, Git filters, and the
standard pre-commit hook need no changes.
Migrating from nb-clean 4
nb-clean was ported from Python to Rust in version 5. PyPI wheels install the
native executable; the Python library API is removed. The installed command
supports Git filters and pre-commit as before.
Use nb-clean --version instead of nb-clean version. To preserve selected
metadata, replace --preserve-cell-metadata tags slideshow -- with
--preserve-cell-metadata-field tags --preserve-cell-metadata-field slideshow.
The bare -m option now accepts no field arguments and can appear before paths.
An execution count of zero is now reported as dirty. Transient metadata is also
reported explicitly. Processing errors use the statuses above, and clean files
are no longer rewritten just to normalise formatting. Unsupported notebook major
versions are rejected.