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, ordist. - 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.pyand changeDRY_RUN = TruetoDRY_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
- Open a terminal in the repository folder (or specify your target directory by editing the script):
cd /path/to/your/project
python3 deleteNodeModules.py
- By default the script starts at
./(the current directory) andmin_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_directoryto the path you want to scan. - Increase
min_depthto avoid deleting shallow matches (for example,min_depth = 1will skip immediate children of the root).
Examples
- Dry run from current directory (default):
python3 deleteNodeModules.py
- Dry run from a custom folder (edit
target_directoryin the file), or run it from that folder:
cd /Users/alice/projects
python3 /path/to/deleteNodeModules.py
- To actually delete, edit
deleteNodeModules.pyand setDRY_RUN = False, then run the script. Double-check the path andmin_depthfirst.
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.rmtreewhenDRY_RUNisFalse— 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0fbaca4131e34b2c5c8746637a2149fb239a61f6d52d7d1dbe023e3389e5ab54
|
|
| MD5 |
cfe69f0408ee950ebc2f77d40e24344e
|
|
| BLAKE2b-256 |
f79c8ea3a9ff32806b577c544580d029418502d6118e98c66c5dc746f1390239
|
File details
Details for the file cleanup_nodemodule-0.1.0-py3-none-any.whl.
File metadata
- Download URL: cleanup_nodemodule-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7673ebcdc152a555b2abfb06b1764f9afa60959f0bb66a227c48d4b053d5ae6
|
|
| MD5 |
7c330d83193d0310f6da4bef42851b62
|
|
| BLAKE2b-256 |
3fd3dc2292254e54c07ccb64cd655373192d036eb5954a8a05beb4d6f0e8215b
|