Skip to content

Instantly share code, notes, and snippets.

@VIRUXE
Created June 22, 2024 15:06
Show Gist options
  • Save VIRUXE/26e38bf5c1c8a4c423eaa213955f7f8d to your computer and use it in GitHub Desktop.
Save VIRUXE/26e38bf5c1c8a4c423eaa213955f7f8d to your computer and use it in GitHub Desktop.
Deletes node_modules folders, recursively.
Param(
[Parameter(Mandatory)]
[string]$RootPath
)
if (-not $RootPath) { $RootPath = Read-Host "Enter the root path to search for 'node_modules' folders" }
function Remove-NodeModules($path) {
$success = $true
Get-ChildItem -Path $path -Directory -Recurse | Where-Object { $_.Name -eq "node_modules" } | ForEach-Object {
try {
Write-Host "Deleting:", $_.FullName
Remove-Item -Recurse -Force -Path $_.FullName -ErrorAction Stop
} catch {
$success = $false
Write-Host "Error deleting '$($_.FullName)'. Attempting partial deletion..."
Remove-NodeModules $_.FullName
}
}
return $success
}
try {
if (-not (Test-Path $RootPath)) { throw "Invalid RootPath provided: $RootPath" }
$success = Remove-NodeModules $RootPath
if (-not $success) { Write-Host "Operation completed with errors." } else { Write-Host "Operation completed successfully." }
} catch {
Write-Error $_.Exception.Message
} finally {
Write-Host "Press any key to exit..."
[Console]::ReadKey($true) | Out-Null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment