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):    
    try:
        with os.scandir(path) as entries:
            for entry in entries:
                if entry.is_dir():
                    if(entry.name == 'node_modules'):
                        all_node_path.append(entry.path)
                    elif(entry.name in block_path):
                        continue
                    else:
                        getNodeModulesPaths(path+'/'+entry.name)
        return 1
    except Exception as e:
        print(f"An error occurred: {e}")
        return []

def remove_directory(directory_path):
    try:
        shutil.rmtree(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()
    getNodeModulesPaths(args.path)
    for path in all_node_path:
        print(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:
            remove_directory(path)
        print("Removed "+str(total_paths)+' node_modules successfully!')
    return 1  
if __name__ == "__main__":
    main()

Enter fullscreen mode Exit fullscreen mode

How to use it

Just run python main.py -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: github.com/kuldeepdev407/rm_node_modules

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

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay