Skip to content

Instantly share code, notes, and snippets.

@daredude
Created June 5, 2016 10:53
Show Gist options
  • Save daredude/045910c5a715c02a3d06362830d045b6 to your computer and use it in GitHub Desktop.
Save daredude/045910c5a715c02a3d06362830d045b6 to your computer and use it in GitHub Desktop.
delete all docker container and images on windows
@echo off
FOR /f "tokens=*" %%i IN ('docker ps -aq') DO docker rm %%i
FOR /f "tokens=*" %%i IN ('docker images --format "{{.ID}}"') DO docker rmi %%i
@ilanl
Copy link

ilanl commented Nov 3, 2020

Saved me lot of headaches !

@pmutua
Copy link

pmutua commented Nov 19, 2020

docker rm $(docker ps -aq)

Doesn't work here was the result

C:\Users\Administrator>docker rm $(docker ps -aq)
unknown shorthand flag: 'a' in -aq)
See 'docker rm --help'.

@carehart
Copy link

pmutua, that doesn't work because it's only valid on Linux or from powershell. You're trying to run it from the Windows command line.

But here are two ways to get it to work:

  • you can literally run that command by passing it TO powershell on the DOS cmd line: powershell "docker rm $(docker ps -aq)"
  • or if running powershell at the cmd line strikes anyone as odd, we can do a variant of the longer command originally offered at the top of this gist, but we don't need to do it in a batch file. This would work: FOR /F %k in ('docker ps -aq') DO docker rm %k

@pmutua
Copy link

pmutua commented Feb 26, 2021

pmutua, that doesn't work because it's only valid on Linux or from powershell. You're trying to run it from the Windows command line.

But here are two ways to get it to work:

  • you can literally run that command by passing it TO powershell on the DOS cmd line: powershell "docker rm $(docker ps -aq)"
  • or if running powershell at the cmd line strikes anyone as odd, we can do a variant of the longer command originally offered at the top of this gist, but we don't need to do it in a batch file. This would work: FOR /F %k in ('docker ps -aq') DO docker rm %k

Thank you

@pgsridhar
Copy link

Thanks ! working !

@suthikhamsamut
Copy link

thanks so much

@quanganh2001
Copy link

Thank you very much! This bat file has solved vmmem high CPU.

@AkryosIT
Copy link

AkryosIT commented Feb 3, 2024

just tried this and it works
foreach ($cnt in $(docker ps -aq)) {docker rm $cnt;Write-Host "$cnt deleted"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment