Skip to main content

Find and remove node_modules, .next and dist folders (dry-run default).

Project description

deleteNodeModules.py

A small utility to find and remove common build/artifact folders such as node_modules, .next, and dist recursively.

What it does

  • Recursively walks from a specified root directory and finds directories named node_modules, .next, or dist.
  • By default the script runs in a safe "dry run" mode where it only prints what would be deleted.

Tested

I tested this on macOS (zsh) and it works fine in dry-run mode. Use with care before disabling dry-run.

Safety / Dry-run

The script has a safety flag at the top of the file:

# Safety: set to True to do a dry run (only log what would be deleted).
# Set to False to actually remove files/directories.
DRY_RUN = True
  • Default: DRY_RUN = True (dry-run).
  • To perform actual deletion, open deleteNodeModules.py and change DRY_RUN = True to DRY_RUN = False.

Important: disabling dry-run will delete files and directories permanently (via shutil.rmtree). Make sure you have backups or use version control and run the script from the correct directory.

Usage

  1. Open a terminal in the repository folder (or specify your target directory by editing the script):
cd /path/to/your/project
python3 deleteNodeModules.py
  1. By default the script starts at ./ (the current directory) and min_depth = 0 (so it will consider matches at any depth). If you want to change those defaults, edit the bottom section of the script:
if __name__ == '__main__':
    target_directory = './'  # Replace with your root folder path
    min_depth = 0  # Minimum depth to start deleting (0 = all levels)
    
    find_and_delete_targets(target_directory, min_depth)
  • Set target_directory to the path you want to scan.
  • Increase min_depth to avoid deleting shallow matches (for example, min_depth = 1 will skip immediate children of the root).

Examples

  • Dry run from current directory (default):
python3 deleteNodeModules.py
  • Dry run from a custom folder (edit target_directory in the file), or run it from that folder:
cd /Users/alice/projects
python3 /path/to/deleteNodeModules.py
  • To actually delete, edit deleteNodeModules.py and set DRY_RUN = False, then run the script. Double-check the path and min_depth first.

Notes & Recommendations

  • Always run the script in dry-run mode first to verify which directories it will remove.
  • Consider adding a CLI wrapper (argparse) if you'd like to toggle dry-run, set target path, and set min depth without editing the file.
  • The script removes directories using shutil.rmtree when DRY_RUN is False — this is irreversible.

Troubleshooting

  • If you hit permission errors, run as a user with the appropriate permissions or adjust ownership/permissions for target folders. Use caution with sudo.

Install & run from terminal (pip)

After publishing to PyPI, developers can install and use the tool directly from the terminal. Recommended steps:

  1. Install via pip (from PyPI):
python3 -m pip install --user cleanup-nodemodule
  1. Make sure your user-level bin directory is on your PATH. On macOS that is commonly $HOME/Library/Python/<pyversion>/bin. Example for zsh/Python 3.9:
export PATH="$HOME/Library/Python/3.9/bin:$PATH"
# add the above line to ~/.zshrc to make it permanent
  1. Run the installed CLI (dry-run is the default):
cleanup-nodemodule --help
cleanup-nodemodule -p /path/to/project      # dry-run: shows what would be deleted
cleanup-nodemodule -p /path/to/project --no-dry-run  # actually deletes targets
  1. To uninstall or upgrade:
python3 -m pip uninstall cleanup-nodemodule
python3 -m pip install --user --upgrade cleanup-nodemodule
  1. If the command cleanup-nodemodule isn't found after installation, either add the user scripts directory to PATH (see step 2) or run the script with the full path, for example:
$HOME/Library/Python/3.9/bin/cleanup-nodemodule --help

Security & behavior reminders

  • The CLI defaults to dry-run (safe). To make the run destructive you must pass --no-dry-run explicitly.
  • Always run a dry-run first and check the output before using --no-dry-run.

If you'd like, I can add a short Examples section showing real sample output from a dry-run and a recommended GitHub Actions workflow to publish on tag.

License / Contribution

This is a small utility — feel free to adapt it. If you'd like, I can add CLI flags (safe defaults) or a confirmation prompt before destructive runs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cleanup_nodemodule-0.1.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cleanup_nodemodule-0.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file cleanup_nodemodule-0.1.1.tar.gz.

File metadata

  • Download URL: cleanup_nodemodule-0.1.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for cleanup_nodemodule-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ffb7ae0e610709309dfda56b3f7d44b3d887eab7a7e6c99e89a53821a329d199
MD5 8c9ed5940abfd002509fc98f0508c94d
BLAKE2b-256 bf127beeb76b59eed765f5706e6395dcdb11254db995089b99355e33ca78e8b4

See more details on using hashes here.

File details

Details for the file cleanup_nodemodule-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cleanup_nodemodule-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 894b4c94b80bb7f2c19f0ccbb175e08deb54ec6babb672d2fd307fca016a850b
MD5 2bf82a87cf7dc29d94b7cf77b5785bfe
BLAKE2b-256 8bb7b1211e3489d4f766921a9aa1ce7242f33f370a764b6d396e0ff4c1aed323

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page