Skip to content

Instantly share code, notes, and snippets.

@brokenthorn
Last active November 15, 2024 13:26
Show Gist options
  • Save brokenthorn/8602b5733782db2a3b2f3ecc35616dbd to your computer and use it in GitHub Desktop.
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)
# 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