Skip to content

Instantly share code, notes, and snippets.

@TheBojda
Created April 7, 2025 06:05
Show Gist options
  • Save TheBojda/12733dc352b664c8acf0cca34015018f to your computer and use it in GitHub Desktop.
Save TheBojda/12733dc352b664c8acf0cca34015018f to your computer and use it in GitHub Desktop.
Cleanup your projects directory by recusively delete node_modules
#!/usr/bin/env python3
import os
import shutil
def delete_node_modules(root_dir):
for dirpath, dirnames, filenames in os.walk(root_dir):
if 'node_modules' in dirnames:
full_path = os.path.join(dirpath, 'node_modules')
try:
print(f"Deleting: {full_path}")
shutil.rmtree(full_path)
print("? Deleted successfully.")
except Exception as e:
print(f"? Failed to delete {full_path}: {e}")
# Remove from dirnames to avoid descending into it
dirnames.remove('node_modules')
if __name__ == "__main__":
current_dir = os.getcwd()
print(f"Starting cleanup in: {current_dir}")
delete_node_modules(current_dir)
print("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment