Skip to content

Instantly share code, notes, and snippets.

@gregmac
Created February 1, 2022 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregmac/b776429944acc82f6fda09216fa7cd12 to your computer and use it in GitHub Desktop.
Save gregmac/b776429944acc82f6fda09216fa7cd12 to your computer and use it in GitHub Desktop.
Cleanup Nuget Cache
# cleanups up the .nuget\packages directory
# * Keeps anything added within 120 days
# * Keeps the most recently used of every package
$maxAge = (Get-Date).AddDays(-120);
$deleted = 0;
$deletedMb = 0;
Get-ChildItem -Directory -Path ([IO.Path]::Combine($HOME, ".nuget", "packages")) | ForEach-Object {
Get-ChildItem -Directory -Path $_.Fullname | Sort-Object -Property LastWriteTime -Descending | ForEach-Object {$i=0} {
$sizeMb = [Math]::Round((Get-ChildItem -Path $_.FullName -Recurse | Measure-Object -Sum Length).Sum / 1MB, 2)
if ($i++ -eq 0) {
Write-Host -ForegroundColor DarkGreen "$($_.FullName) skipped (first) $sizeMb MB";
} elseif ($_.LastWriteTime -gt $maxAge) {
Write-Host -ForegroundColor DarkCyan "$($_.FullName) skipped (last write $($_.LastWriteTime)) $sizeMb MB";
} else {
Write-Host -ForegroundColor DarkRed "$($_.FullName) deleted (last write $($_.LastWriteTime)) $sizeMb MB" ;
Remove-Item $_.FullName -Recurse
$deletedMb += $sizeMb;
$deleted += 1;
}
}
}
Write-Host -ForegroundColor Red "Deleted $deleted package versions, $deletedMb MB";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment