Free Up Disk Space: A Step-by-Step Guide to Removing ‘node_modules’ Folders in PowerShell
If you’re a developer working with Node.js, you’ve likely encountered the “node_modules” folder. This folder contains all the dependencies for your Node.js projects and can quickly grow to occupy a significant amount of disk space.
If you have multiple Node.js projects or repositories, you may have multiple “node_modules” folders scattered throughout your file system. This can make it difficult to manage disk space and may cause performance issues.
You can use PowerShell to find and delete all “node_modules” folders under a root folder. However, it’s important to note that deleting files or folders can be risky and may result in the loss of data. Therefore, it’s recommended that you back up your data before proceeding with the steps outlined below.
Here’s how to find and delete all “node_modules” folders under a root folder using PowerShell:
Step 1: Open PowerShell
Open PowerShell by typing “PowerShell” into the Windows search bar and clicking on the “Windows PowerShell” app.
Step 2: Navigate to the root folder
Use the cd
command to navigate to the root folder where you want to search for "node_modules" folders. For example, if your root folder is located at "C:\projects", type the following command:
cd C:\projects
Step 3: Find all “node_modules” folders
To find all “node_modules” folders under the root folder, you can use the Get-ChildItem
cmdlet with the -Recurse
and -Directory
parameters. You can also add a filter to exclude any node_modules
folders that are nested inside another node_modules
folder.
Here’s the PowerShell command to find all top-level node_modules
folders:
Get-ChildItem -Path . -Recurse -Directory -Filter "node_modules" | Where-Object { $_.FullName -notmatch '\\node_modules\\' } | Select-Object -ExpandProperty FullName
This command will search the current directory and all subdirectories for all top-level node_modules
folders and output their full paths. The Where-Object
cmdlet is used to exclude any node_modules
folders that are nested inside another node_modules
folder, and the Select-Object
cmdlet is used to extract the FullName
property for each folder.
Note that this command can take some time to run if you have a large directory tree with many node_modules
folders. You should also use caution when deleting these folders, as doing so may break any applications or scripts that depend on them.
Step 4: Delete all “node_modules” folders
Use the Remove-Item
cmdlet to delete all "node_modules" folders found in Step 3. The -Recurse
parameter tells PowerShell to delete all files and subdirectories within the "node_modules" folders, and the -Force
parameter tells PowerShell to force the deletion of read-only files and hidden files.
Get-ChildItem -Path . -Recurse -Directory -Filter "node_modules" | Remove-Item -Recurse -Force
This will delete all “node_modules” folders and their contents under the root folder.
Step 5: Verify deletion
Use the Get-ChildItem
cmdlet again to verify that all "node_modules" folders have been deleted.
Get-ChildItem -Path . -Recurse -Directory -Filter "node_modules" | Where-Object { $_.FullName -notmatch '\\node_modules\\' } | Select-Object -ExpandProperty FullNameThis should return an empty list, indicating that all "node_modules" folders have been deleted.
Conclusion
Using PowerShell to find and delete all "node_modules" folders under a root folder can help you manage disk space and improve performance. However, it's important to remember that deleting files or folders can be risky and may result in the loss of data. Therefore, it's recommended that you back up your data before proceeding with the steps outlined above. By following the steps carefully and proceeding at your own risk, you can quickly and easily clean up your file system.