Skip to content

Instantly share code, notes, and snippets.

@ngpestelos
Last active March 5, 2024 20:45
Show Gist options
  • Save ngpestelos/4fc2e31e19f86b9cf10b to your computer and use it in GitHub Desktop.
Save ngpestelos/4fc2e31e19f86b9cf10b to your computer and use it in GitHub Desktop.
How to remove unused Docker containers and images

May 8, 2018

I wrote this four years ago, so instead use this command:

$ docker rmi $(docker images -q -f dangling=true)
@jfsdcgy
Copy link

jfsdcgy commented Sep 12, 2018

'docker system prune' doesn't remove images on windows

same here, have you found any solution?

@MagicJohnJang
Copy link

For Windows, what's more:

Stop and remove by image name =>

FOR /f "tokens=*" %i IN ('docker ps -a --filter "ancestor=ImageNameHere" -q') DO docker stop %i && docker rm %i

...

(don't forget to change %i to %%i in batch file)

@RaimundasR
Copy link

RaimundasR commented Mar 17, 2022

the best thing to control and clean up unused containers which still running are to label them on docker run and use few command lines through crontab:
1'st one to kill docker containers creates x time ago and with labe x:
docker ps -a --filter "label=<label_name>" | grep 'x period crated ago' | awk '{ print $1 }' | xargs -I {} docker rm {} -f
2'nd one clean up all unused images which also have tag "none":
docker rmi $(docker images --filter "dangling=true" -q --no-trunc) 2>/dev/null

Then your environment should be always clean :) Cheers....

@Kejk23
Copy link

Kejk23 commented Jul 31, 2022

docker ps -a -q | % { docker rm $_ }
docker images -q | % { docker rmi $_ }

thank you so much

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