DEV Community

Kuldeep Singh
Kuldeep Singh

Posted on

Remove all node_modules folder from PC

If you ever thing that there are a lot of project you are working and you have lot of node_modules folder you have to delete to save up space when you only have 512SSD.

Here is a python script for deleting all those node_modules folder.

import os
import argparse
import shutil

all_node_path = []
block_path = ['$RECYCLE.BIN','System Volume Information']

def getNodeModulesPaths(path):    
        with os.scandir(path) as entries:
            for entry in entries:
                if entry.is_dir():
                    if( == 'node_modules'):
                    elif( in block_path):
        return 1
    except Exception as e:
        print(f"An error occurred: {e}")
        return []

def remove_directory(directory_path):
        print(f"'{directory_path}' removed successfully.")
    except OSError as e:
        print(f"Error: {directory_path} : {e.strerror}")
def main():
    # arguments
    args_parser = argparse.ArgumentParser(description="CLI tool for removing all node_module folder for given path")
    args_parser.add_argument('-p', '--path', required=True, type=str,help="full path from where you want to remove node_module ")

    args = args_parser.parse_args()
    for path in all_node_path:

    total_paths = len(all_node_path)

    confirm_rm = input("Are you sure you want to delete above "+str(total_paths)+" folder(y/n):").lower()
    if(confirm_rm == 'y'):
        for path in all_node_path:
        print("Removed "+str(total_paths)+' node_modules successfully!')
    return 1  
if __name__ == "__main__":

Enter fullscreen mode Exit fullscreen mode

How to use it

Just run python -p '<pathtofolder>' it is going to scan all node_modules folder in path and going to delete them.

Run it carefully also check the folders before confirming

Here is GitHub link for repo:

If i missed something in code feel free to create Issue Or PR.

Top comments (0)