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.

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.0.tar.gz (4.7 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.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cleanup_nodemodule-0.1.0.tar.gz
  • Upload date:
  • Size: 4.7 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.0.tar.gz
Algorithm Hash digest
SHA256 0fbaca4131e34b2c5c8746637a2149fb239a61f6d52d7d1dbe023e3389e5ab54
MD5 cfe69f0408ee950ebc2f77d40e24344e
BLAKE2b-256 f79c8ea3a9ff32806b577c544580d029418502d6118e98c66c5dc746f1390239

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cleanup_nodemodule-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7673ebcdc152a555b2abfb06b1764f9afa60959f0bb66a227c48d4b053d5ae6
MD5 7c330d83193d0310f6da4bef42851b62
BLAKE2b-256 3fd3dc2292254e54c07ccb64cd655373192d036eb5954a8a05beb4d6f0e8215b

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