Created
April 7, 2025 06:05
-
-
Save TheBojda/12733dc352b664c8acf0cca34015018f to your computer and use it in GitHub Desktop.
Cleanup your projects directory by recusively delete node_modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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