mayankjoin3

Remove-All-Docker-Images.ps1

Jun 26th, 2026
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     Remove-All-Docker-Images.ps1
  3.  
  4.     Purpose:
  5.     - Show all Docker images installed on Windows
  6.     - Show image sizes
  7.     - Stop and remove containers that may block image removal
  8.     - Remove all Docker images
  9.     - Run safe Docker cleanup
  10.     - Show disk usage before and after
  11.  
  12.     This script removes:
  13.     - Docker images
  14.     - Stopped/running containers, if you allow it
  15.     - Dangling build cache
  16.     - Unused networks
  17.  
  18.     This script does NOT remove by default:
  19.     - Docker volumes
  20.     - Named database volumes
  21.     - Bind-mounted project folders
  22.     - Docker Desktop itself
  23.  
  24.     WARNING:
  25.     If your MySQL/Postgres/Mongo data is stored in Docker volumes,
  26.     do NOT enable volume pruning unless you are absolutely sure.
  27.  
  28.     Run:
  29.     powershell -ExecutionPolicy Bypass -File .\Remove-All-Docker-Images.ps1
  30. #>
  31.  
  32. Write-Host ""
  33. Write-Host "==========================================" -ForegroundColor Cyan
  34. Write-Host " Docker Image Removal Tool - Windows " -ForegroundColor Cyan
  35. Write-Host "==========================================" -ForegroundColor Cyan
  36. Write-Host ""
  37.  
  38. # -----------------------------
  39. # Check Docker availability
  40. # -----------------------------
  41.  
  42. $dockerExists = Get-Command docker -ErrorAction SilentlyContinue
  43.  
  44. if (-not $dockerExists) {
  45.     Write-Host "Docker command not found." -ForegroundColor Red
  46.     Write-Host "Make sure Docker Desktop is installed and running."
  47.     exit 1
  48. }
  49.  
  50. try {
  51.     docker version | Out-Null
  52. }
  53. catch {
  54.     Write-Host "Docker is installed but Docker Engine is not responding." -ForegroundColor Red
  55.     Write-Host "Start Docker Desktop and run this script again."
  56.     exit 1
  57. }
  58.  
  59. # -----------------------------
  60. # Show Docker disk usage before
  61. # -----------------------------
  62.  
  63. Write-Host "Current Docker disk usage:" -ForegroundColor Green
  64. docker system df
  65. Write-Host ""
  66.  
  67. # -----------------------------
  68. # Show installed images
  69. # -----------------------------
  70.  
  71. Write-Host "Installed Docker images:" -ForegroundColor Green
  72. Write-Host ""
  73.  
  74. $images = docker images --format "{{.Repository}}|{{.Tag}}|{{.ID}}|{{.CreatedSince}}|{{.Size}}"
  75.  
  76. if (-not $images) {
  77.     Write-Host "No Docker images found." -ForegroundColor Yellow
  78. }
  79. else {
  80.     $imageObjects = $images | ForEach-Object {
  81.         $parts = $_ -split "\|"
  82.         [PSCustomObject]@{
  83.             Repository = $parts[0]
  84.             Tag        = $parts[1]
  85.             ImageID    = $parts[2]
  86.             Created    = $parts[3]
  87.             Size       = $parts[4]
  88.         }
  89.     }
  90.  
  91.     $imageObjects | Format-Table -AutoSize
  92. }
  93.  
  94. Write-Host ""
  95. Write-Host "All containers:" -ForegroundColor Green
  96. docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Names}}\t{{.Status}}\t{{.Size}}"
  97.  
  98. Write-Host ""
  99. Write-Host "This operation will remove ALL Docker images." -ForegroundColor Red
  100. Write-Host "Containers using those images may also need to be stopped and removed." -ForegroundColor Yellow
  101. Write-Host ""
  102.  
  103. $confirm = Read-Host "Type DELETE to continue"
  104.  
  105. if ($confirm -ne "DELETE") {
  106.     Write-Host "Aborted. Nothing removed." -ForegroundColor Yellow
  107.     exit 0
  108. }
  109.  
  110. # -----------------------------
  111. # Ask whether to remove containers
  112. # -----------------------------
  113.  
  114. Write-Host ""
  115. $removeContainers = Read-Host "Remove all containers also? Required if images are in use. Type YES to remove containers"
  116.  
  117. if ($removeContainers -eq "YES") {
  118.     Write-Host ""
  119.     Write-Host "Stopping running containers..." -ForegroundColor Yellow
  120.  
  121.     $runningContainers = docker ps -q
  122.  
  123.     if ($runningContainers) {
  124.         docker stop $runningContainers
  125.     }
  126.     else {
  127.         Write-Host "No running containers found."
  128.     }
  129.  
  130.     Write-Host ""
  131.     Write-Host "Removing all containers..." -ForegroundColor Yellow
  132.  
  133.     $allContainers = docker ps -aq
  134.  
  135.     if ($allContainers) {
  136.         docker rm -f $allContainers
  137.     }
  138.     else {
  139.         Write-Host "No containers found."
  140.     }
  141. }
  142. else {
  143.     Write-Host "Skipping container removal." -ForegroundColor Yellow
  144.     Write-Host "Images used by existing containers may fail to delete."
  145. }
  146.  
  147. # -----------------------------
  148. # Remove all Docker images
  149. # -----------------------------
  150.  
  151. Write-Host ""
  152. Write-Host "Collecting image IDs..." -ForegroundColor Green
  153.  
  154. $imageIds = docker images -aq | Sort-Object -Unique
  155.  
  156. if (-not $imageIds) {
  157.     Write-Host "No images to remove." -ForegroundColor Yellow
  158. }
  159. else {
  160.     Write-Host "Removing Docker images..." -ForegroundColor Red
  161.  
  162.     foreach ($img in $imageIds) {
  163.         Write-Host "Removing image: $img" -ForegroundColor Gray
  164.         docker rmi -f $img
  165.     }
  166. }
  167.  
  168. # -----------------------------
  169. # Builder cache cleanup
  170. # -----------------------------
  171.  
  172. Write-Host ""
  173. Write-Host "Cleaning Docker builder cache..." -ForegroundColor Green
  174. docker builder prune -a -f
  175.  
  176. # -----------------------------
  177. # System prune
  178. # -----------------------------
  179. # This removes:
  180. # - stopped containers
  181. # - unused networks
  182. # - dangling images
  183. # - build cache
  184. #
  185. # It does NOT remove volumes unless --volumes is used.
  186.  
  187. Write-Host ""
  188. Write-Host "Running docker system prune..." -ForegroundColor Green
  189. docker system prune -a -f
  190.  
  191. # -----------------------------
  192. # Optional volume prune
  193. # -----------------------------
  194.  
  195. Write-Host ""
  196. Write-Host "Docker volumes are NOT removed by default." -ForegroundColor Yellow
  197. Write-Host "Volumes may contain database data such as MySQL, PostgreSQL, MongoDB, etc." -ForegroundColor Yellow
  198.  
  199. $pruneVolumes = Read-Host "Type VOLUMES to also remove unused Docker volumes"
  200.  
  201. if ($pruneVolumes -eq "VOLUMES") {
  202.     Write-Host ""
  203.     Write-Host "Removing unused Docker volumes..." -ForegroundColor Red
  204.     docker volume prune -f
  205. }
  206. else {
  207.     Write-Host "Skipping Docker volume cleanup." -ForegroundColor Yellow
  208. }
  209.  
  210. # -----------------------------
  211. # Show final status
  212. # -----------------------------
  213.  
  214. Write-Host ""
  215. Write-Host "Remaining Docker images:" -ForegroundColor Green
  216. docker images
  217.  
  218. Write-Host ""
  219. Write-Host "Remaining Docker containers:" -ForegroundColor Green
  220. docker ps -a
  221.  
  222. Write-Host ""
  223. Write-Host "Final Docker disk usage:" -ForegroundColor Green
  224. docker system df
  225.  
  226. Write-Host ""
  227. Write-Host "Done." -ForegroundColor Cyan
Advertisement
Add Comment
Please, Sign In to add comment