Skip to content

Instantly share code, notes, and snippets.

@SH20RAJ
Created November 24, 2024 08:26
Show Gist options
  • Save SH20RAJ/f3bfc2ddddcef5eb43ec7e93375cc3b6 to your computer and use it in GitHub Desktop.
Save SH20RAJ/f3bfc2ddddcef5eb43ec7e93375cc3b6 to your computer and use it in GitHub Desktop.
Delete all node_modules folders in the system
import os
import shutil
def delete_node_modules(start_path):
"""
Deletes all 'node_modules' folders starting from the given path.
:param start_path: Directory path to begin the search.
"""
deleted_count = 0
for root, dirs, files in os.walk(start_path):
# Check if 'node_modules' is in the current directory's subdirectories
if 'node_modules' in dirs:
node_modules_path = os.path.join(root, 'node_modules')
try:
# Remove the directory
shutil.rmtree(node_modules_path)
print(f"Deleted: {node_modules_path}")
deleted_count += 1
except Exception as e:
print(f"Failed to delete {node_modules_path}: {e}")
print(f"\nTotal 'node_modules' folders deleted: {deleted_count}")
if __name__ == "__main__":
# Get the starting directory from the user
start_directory = input("Enter the directory to start searching for 'node_modules' (e.g., C:\\ or /): ").strip()
if os.path.exists(start_directory):
delete_node_modules(start_directory)
else:
print("The provided directory does not exist!")
# pip install send2trash
import os
import shutil
from send2trash import send2trash
def find_node_modules(start_path):
"""
Finds all 'node_modules' folders starting from the given path.
:param start_path: Directory path to begin the search.
:return: List of paths to 'node_modules' folders.
"""
node_modules_paths = []
for root, dirs, files in os.walk(start_path):
if 'node_modules' in dirs:
node_modules_paths.append(os.path.join(root, 'node_modules'))
return node_modules_paths
def delete_folders(folders, permanently=True):
"""
Deletes the specified folders either permanently or moves them to the recycle bin.
:param folders: List of folder paths to delete.
:param permanently: If True, deletes permanently. If False, moves to recycle bin.
"""
for folder in folders:
try:
if permanently:
shutil.rmtree(folder)
print(f"Deleted permanently: {folder}")
else:
send2trash(folder)
print(f"Moved to recycle bin: {folder}")
except Exception as e:
print(f"Error deleting {folder}: {e}")
if __name__ == "__main__":
# Get the starting directory from the user
start_directory = input("Enter the directory to start searching for 'node_modules' (e.g., C:\\ or /): ").strip()
if os.path.exists(start_directory):
print("Scanning for 'node_modules' folders. This may take some time...")
node_modules_paths = find_node_modules(start_directory)
if node_modules_paths:
print(f"\nFound {len(node_modules_paths)} 'node_modules' folders:")
for idx, path in enumerate(node_modules_paths, start=1):
print(f"{idx}. {path}")
# Confirm before proceeding
confirm = input("\nDo you want to delete these folders? (yes/no): ").strip().lower()
if confirm == 'yes':
# Choose delete mode
mode = input("Delete permanently or move to recycle bin? (permanent/recycle): ").strip().lower()
permanently = mode == 'permanent'
print("\nDeleting folders...")
delete_folders(node_modules_paths, permanently)
print("\nProcess complete!")
else:
print("No folders were deleted.")
else:
print("No 'node_modules' folders found.")
else:
print("The provided directory does not exist!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment