Skip to content

Instantly share code, notes, and snippets.

@ngpestelos
Last active March 5, 2024 20:45
Star You must be signed in to star a gist
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)
@rpratesh
Copy link

rpratesh commented Feb 27, 2018

This worked for me on Ubuntu 16.04
Before deleting all the containers, force stop them:
docker ps -q -a | xargs docker stop

Then delete the containers using:
docker ps -q -a | xargs docker rm

Now delete all the dangling images using:
docker rmi $(docker images | grep “^<none>” | awk ‘{print $3}’)

@stiv-yakovenko
Copy link

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

@Barathchander
Copy link

You can try out below script, i am using that below

#!/bin/bash

timestamp=$(date +%Y%m%d_%H%M%S)
log_path="`pwd`"
filename=docker_cleanup_$timestamp.log
log=$log_path/$filename


docker_space_before(){
CURRENTSPACE=`docker system df`
echo "Current Docker Space:" >> $log
echo $CURRENTSPACE >>$log
}
docker_find (){
echo "#####################################################################" >> $log
echo "Finding images" >> $log
echo "#####################################################################" >> $log
REMOVEIMAGES=`docker images | grep " [days|months|weeks]* ago" | awk '{print $3}'`

echo "Listing images that needs to be cleaned up" >> $log
echo $REMOVEIMAGES >>$log

}

docker_cleanup(){
echo "#####################################################################" >> $log
echo "Cleaning images" >> $log
echo "#####################################################################" >> $log
docker rmi ${REMOVEIMAGES}
}

docker_space_after(){
CURRENTSPACE=`docker system df`
echo "Current Docker Space, after clean up:" >> $log
echo $CURRENTSPACE >>$log
}
docker_space_before
docker_find
docker_cleanup
docker_space_after

@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