Skip to content

Instantly share code, notes, and snippets.

@Maxiviper117
Created February 2, 2024 08:25
Show Gist options
  • Save Maxiviper117/1825f619890e0d7953a58a81920049d2 to your computer and use it in GitHub Desktop.
Save Maxiviper117/1825f619890e0d7953a58a81920049d2 to your computer and use it in GitHub Desktop.
Delete all node_modules in a specified directory using Python
from pathlib import Path
import shutil
def delete_node_modules(directory):
deleted_any = False # Track if any node_modules directories have been deleted
for path in directory.rglob('*'): # Use rglob to find all paths
if path.name == 'node_modules' and path.is_dir():
shutil.rmtree(path)
print(f"Deleted {path}")
deleted_any = True
return deleted_any
# Example usage
directory_to_search = Path('/path/to/your/directory')
if not delete_node_modules(directory_to_search):
print("No node_modules directory found.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment