Last active
November 15, 2024 13:26
-
-
Save brokenthorn/8602b5733782db2a3b2f3ecc35616dbd to your computer and use it in GitHub Desktop.
Delete all node_modules folders recursively from current folder (or any other folder if you change the folder name)
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
# Recursively search for directories named "node_modules" | |
Get-ChildItem -Recurse -Directory -Filter "node_modules" | ForEach-Object { | |
# Check if the directory name is exactly "node_modules" | |
if ($_.Name -eq "node_modules" -and $_.FullName -notmatch "node_modules.*\\node_modules") { | |
# Output the path of the directory being deleted | |
Write-Output "Deleting: $($_.FullName)" | |
# Use the 'rd' command via cmd.exe to delete the directory quietly and efficiently | |
# /s: Removes all files and subdirectories | |
# /q: Suppresses confirmation prompts | |
cmd.exe /c "rd /s /q `"$($_.FullName)`"" | |
} | |
} | |
# Using rd is faster than other solutions because it handles bulk copies better. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment