-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Remove-DockerTrash.ps1
46 lines (39 loc) · 1.07 KB
/
Remove-DockerTrash.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<#
.SYNOPSIS
Prune unused docker containers and dangling images.
.PARAMETER Volumes
Prune unused volumes.
#>
param ([switch] $Volumes)
if (!(Test-Elevated (Split-Path -Leaf $PSCommandPath) -warn)) { return }
Write-Host
$trash = $(docker ps -q -f "status=exited")
if ($trash -ne $null) {
Write-Host ('Removing {0} stopped containers...' -f $trash.Count) -ForegroundColor DarkYellow
docker container prune -f
}
else {
Write-Host "No stopped containers" -ForegroundColor DarkYellow
}
Write-Host
$trash = $(docker images --filter "dangling=true" -q --no-trunc)
if ($trash -ne $null) {
Write-Host ('Removing {0} dangling images...' -f $trash.Count) -ForegroundColor DarkYellow
docker rmi $trash
}
else {
Write-Host "No dangling images" -ForegroundColor DarkYellow
}
if ($Volumes)
{
Write-Host
$trash = $(docker volume ls --filter "dangling=true" -q)
if ($trash -ne $null) {
Write-Host ('Removing {0} dangling volumes...' -f $trash.Count) -ForegroundColor DarkYellow
docker volume prune -f
}
else {
Write-Host "No dangling volumes" -ForegroundColor DarkYellow
}
}
Write-Host