Here are two common ways to wipe every output from every .ipynb file inside a directory tree.
────────────────────────────────────────
- One-liner (Bash + Jupyter built-ins)
────────────────────────────────────────
If you already have Jupyter installed, the easiest approach is a single command:
find /path/to/root -name '*.ipynb' -print0 \
| xargs -0 jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace --to notebook
What it does
• find … -name '*.ipynb' → recurses through all sub-folders.
• xargs -0 → handles spaces/newlines in filenames.
• jupyter nbconvert … --inplace clears the outputs and writes the cleaned notebook back over the original file.
Tip: add --exclude='*checkpoint*' to find if you don’t want to touch Jupyter’s checkpoint files.
────────────────────────────────────────
2) A small, reusable Python script
────────────────────────────────────────
Save this as clear_outputs.py (Python 3):
#!/usr/bin/env python3
"""
Recursively strip all cell outputs & execution counts from .ipynb files.
Usage:
python clear_outputs.py /path/to/root
"""
import sys, pathlib, nbformat
from nbconvert.preprocessors import ClearOutputPreprocessor
def clear_notebook(nb_path: pathlib.Path):
nb = nbformat.read(nb_path, as_version=nbformat.NO_CONVERT)
ClearOutputPreprocessor().preprocess(nb, {}) # modifies nb in-memory
nbformat.write(nb, nb_path) # overwrite original file
print(f"Cleared: {nb_path}")
def walk_and_clear(root: pathlib.Path):
for nb_path in root.rglob("*.ipynb"):
# Skip .ipynb_checkpoints (optional)
if ".ipynb_checkpoints" in nb_path.parts:
continue
clear_notebook(nb_path)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python clear_outputs.py <directory>")
sys.exit(1)
walk_and_clear(pathlib.Path(sys.argv[1]).expanduser().resolve())
Quick run:
pip install nbformat nbconvert # if they’re not installed yet
python clear_outputs.py .
────────────────────────────────────────
3) Git-centric alternative: nbstripout
────────────────────────────────────────
If the reason for stripping outputs is version control noise, just drop nbstripout into your repo:
pip install nbstripout
cd /path/to/repo
nbstripout --install # activates a Git filter
Now every commit will automatically store notebooks without outputs, and you can keep full-output copies locally with:
nbstripout --install --attributes --keep-output # per-developer opt-in
Choose whichever approach fits your workflow best. Happy cleaning!