Skip to content

Instantly share code, notes, and snippets.

@joanjane
Last active March 22, 2021 22:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joanjane/1d08b710edbfcfb66ee32e85e5e4d74c to your computer and use it in GitHub Desktop.
Save joanjane/1d08b710edbfcfb66ee32e85e5e4d74c to your computer and use it in GitHub Desktop.
Powershell script to clean development packages and binaries recursively (node_modules, nuget packages...)
# WARNING:
# IF YOU ARE NOT SURE OF WHAT THIS SCRIPT DOES, DO A BACKUP FIRST.
# THIS CANNOT BE UNDONE, SO BE AWARE OF ANY POSSIBLE LOST FILES.
# Run this on a safe folder where you know the contents, DO NOT RUN THIS IN A FOLDER LIKE "Program Files"!
# I take no responsability of any lost files or damages related to this script.
#
# Instructions:
# This script will delete bin, obj, node_modules and packages folders recursively from the current folder. You can specify the path and folders to delete with -path and -folderNames options.
# Options:
# -path (Optional): The path to delete subfolders recursively, by default the current folder.
# -folderNames (Optional): Folder names to match recursively under $path parameter. By default (bin,obj,node_modules,packages).
# -listMode (Optional): To test which files will be deleted, use -listMode flag to list the folders without deleting.
# -confirm (Optional): Run delete script without confirm prompt
param(
[string]$path = ".\",
[string[]] $folderNames = @('bin', 'obj', 'node_modules', 'packages', '.vs', '.gradle'),
[switch]$confirm = $false,
[switch]$listMode = $false)
function ListFolders([string]$path, [string[]] $folderNames) {
Get-ChildItem $path -include $folderNames -Recurse -Directory -Force
}
function Confirm($message, $confirm) {
if ($confirm -eq $false) {
$confirmation = Read-Host "$message [y/n]"
while ($confirmation -ne "y") {
if ($confirmation -eq 'n') { exit }
$confirmation = Read-Host $message
}
}
}
if ($listMode -eq $false) {
Confirm -message "Are you sure you want to delete $folderNames folders recursively from the current folder?" -confirm:$confirm
}
ListFolders $path $folderNames | foreach ($_) {
if ($listMode -eq $true) {
Write-Host $_.fullname
} else {
Write-Host "Deleting '$($_.fullname)'"
Remove-Item $_.fullname -Force -Recurse
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment