Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- Remove-All-Docker-Images.ps1
- Purpose:
- - Show all Docker images installed on Windows
- - Show image sizes
- - Stop and remove containers that may block image removal
- - Remove all Docker images
- - Run safe Docker cleanup
- - Show disk usage before and after
- This script removes:
- - Docker images
- - Stopped/running containers, if you allow it
- - Dangling build cache
- - Unused networks
- This script does NOT remove by default:
- - Docker volumes
- - Named database volumes
- - Bind-mounted project folders
- - Docker Desktop itself
- WARNING:
- If your MySQL/Postgres/Mongo data is stored in Docker volumes,
- do NOT enable volume pruning unless you are absolutely sure.
- Run:
- powershell -ExecutionPolicy Bypass -File .\Remove-All-Docker-Images.ps1
- #>
- Write-Host ""
- Write-Host "==========================================" -ForegroundColor Cyan
- Write-Host " Docker Image Removal Tool - Windows " -ForegroundColor Cyan
- Write-Host "==========================================" -ForegroundColor Cyan
- Write-Host ""
- # -----------------------------
- # Check Docker availability
- # -----------------------------
- $dockerExists = Get-Command docker -ErrorAction SilentlyContinue
- if (-not $dockerExists) {
- Write-Host "Docker command not found." -ForegroundColor Red
- Write-Host "Make sure Docker Desktop is installed and running."
- exit 1
- }
- try {
- docker version | Out-Null
- }
- catch {
- Write-Host "Docker is installed but Docker Engine is not responding." -ForegroundColor Red
- Write-Host "Start Docker Desktop and run this script again."
- exit 1
- }
- # -----------------------------
- # Show Docker disk usage before
- # -----------------------------
- Write-Host "Current Docker disk usage:" -ForegroundColor Green
- docker system df
- Write-Host ""
- # -----------------------------
- # Show installed images
- # -----------------------------
- Write-Host "Installed Docker images:" -ForegroundColor Green
- Write-Host ""
- $images = docker images --format "{{.Repository}}|{{.Tag}}|{{.ID}}|{{.CreatedSince}}|{{.Size}}"
- if (-not $images) {
- Write-Host "No Docker images found." -ForegroundColor Yellow
- }
- else {
- $imageObjects = $images | ForEach-Object {
- $parts = $_ -split "\|"
- [PSCustomObject]@{
- Repository = $parts[0]
- Tag = $parts[1]
- ImageID = $parts[2]
- Created = $parts[3]
- Size = $parts[4]
- }
- }
- $imageObjects | Format-Table -AutoSize
- }
- Write-Host ""
- Write-Host "All containers:" -ForegroundColor Green
- docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Names}}\t{{.Status}}\t{{.Size}}"
- Write-Host ""
- Write-Host "This operation will remove ALL Docker images." -ForegroundColor Red
- Write-Host "Containers using those images may also need to be stopped and removed." -ForegroundColor Yellow
- Write-Host ""
- $confirm = Read-Host "Type DELETE to continue"
- if ($confirm -ne "DELETE") {
- Write-Host "Aborted. Nothing removed." -ForegroundColor Yellow
- exit 0
- }
- # -----------------------------
- # Ask whether to remove containers
- # -----------------------------
- Write-Host ""
- $removeContainers = Read-Host "Remove all containers also? Required if images are in use. Type YES to remove containers"
- if ($removeContainers -eq "YES") {
- Write-Host ""
- Write-Host "Stopping running containers..." -ForegroundColor Yellow
- $runningContainers = docker ps -q
- if ($runningContainers) {
- docker stop $runningContainers
- }
- else {
- Write-Host "No running containers found."
- }
- Write-Host ""
- Write-Host "Removing all containers..." -ForegroundColor Yellow
- $allContainers = docker ps -aq
- if ($allContainers) {
- docker rm -f $allContainers
- }
- else {
- Write-Host "No containers found."
- }
- }
- else {
- Write-Host "Skipping container removal." -ForegroundColor Yellow
- Write-Host "Images used by existing containers may fail to delete."
- }
- # -----------------------------
- # Remove all Docker images
- # -----------------------------
- Write-Host ""
- Write-Host "Collecting image IDs..." -ForegroundColor Green
- $imageIds = docker images -aq | Sort-Object -Unique
- if (-not $imageIds) {
- Write-Host "No images to remove." -ForegroundColor Yellow
- }
- else {
- Write-Host "Removing Docker images..." -ForegroundColor Red
- foreach ($img in $imageIds) {
- Write-Host "Removing image: $img" -ForegroundColor Gray
- docker rmi -f $img
- }
- }
- # -----------------------------
- # Builder cache cleanup
- # -----------------------------
- Write-Host ""
- Write-Host "Cleaning Docker builder cache..." -ForegroundColor Green
- docker builder prune -a -f
- # -----------------------------
- # System prune
- # -----------------------------
- # This removes:
- # - stopped containers
- # - unused networks
- # - dangling images
- # - build cache
- #
- # It does NOT remove volumes unless --volumes is used.
- Write-Host ""
- Write-Host "Running docker system prune..." -ForegroundColor Green
- docker system prune -a -f
- # -----------------------------
- # Optional volume prune
- # -----------------------------
- Write-Host ""
- Write-Host "Docker volumes are NOT removed by default." -ForegroundColor Yellow
- Write-Host "Volumes may contain database data such as MySQL, PostgreSQL, MongoDB, etc." -ForegroundColor Yellow
- $pruneVolumes = Read-Host "Type VOLUMES to also remove unused Docker volumes"
- if ($pruneVolumes -eq "VOLUMES") {
- Write-Host ""
- Write-Host "Removing unused Docker volumes..." -ForegroundColor Red
- docker volume prune -f
- }
- else {
- Write-Host "Skipping Docker volume cleanup." -ForegroundColor Yellow
- }
- # -----------------------------
- # Show final status
- # -----------------------------
- Write-Host ""
- Write-Host "Remaining Docker images:" -ForegroundColor Green
- docker images
- Write-Host ""
- Write-Host "Remaining Docker containers:" -ForegroundColor Green
- docker ps -a
- Write-Host ""
- Write-Host "Final Docker disk usage:" -ForegroundColor Green
- docker system df
- Write-Host ""
- Write-Host "Done." -ForegroundColor Cyan
Advertisement
Add Comment
Please, Sign In to add comment