Created
February 1, 2022 20:21
-
-
Save gregmac/b776429944acc82f6fda09216fa7cd12 to your computer and use it in GitHub Desktop.
Cleanup Nuget Cache
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
# 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