How to remove old Docker containers
This question is related to Should I be concerned about excess, non-running, Docker containers?.
I'm wondering how to remove old containers. The docker rm 3e552code34a
lets you remove a single one, but I have lots already. docker rm --help
doesn't give a selection option (like all, or by image name).
Maybe there is a directory in which these containers are stored where I can delete them easily manually?
docker
|
show 3 more comments
This question is related to Should I be concerned about excess, non-running, Docker containers?.
I'm wondering how to remove old containers. The docker rm 3e552code34a
lets you remove a single one, but I have lots already. docker rm --help
doesn't give a selection option (like all, or by image name).
Maybe there is a directory in which these containers are stored where I can delete them easily manually?
docker
21
You should also consider cleaning orphaned docker volumes. I often find that they consume much more space than old containers and old images. Good script for removing orphaned docker volumes is available at: github.com/chadoe/docker-cleanup-volumes.
– Nemanja Trifunovic
Dec 1 '15 at 18:06
Maybe github.com/chadoe/docker-cleanup-volumes can help you.
– user1929959
Sep 13 '16 at 19:34
You can also usedocker run
with the--rm
flag which would make the container ephemeral, removing all container files after the run.
– Gordon
Sep 21 '16 at 5:51
12
With docker 1.13 (Q4 2016), you can also consider the newdocker system prune
command. See my answer below.
– VonC
Oct 4 '16 at 19:52
1
Use the docker management toolPortainer
We can manageall the old containers, non using volumes and images
by using this tool Its a simple management UI for dockersPlease refer my update below on how to deploy the application
– Anish Varghese
Nov 22 '18 at 4:47
|
show 3 more comments
This question is related to Should I be concerned about excess, non-running, Docker containers?.
I'm wondering how to remove old containers. The docker rm 3e552code34a
lets you remove a single one, but I have lots already. docker rm --help
doesn't give a selection option (like all, or by image name).
Maybe there is a directory in which these containers are stored where I can delete them easily manually?
docker
This question is related to Should I be concerned about excess, non-running, Docker containers?.
I'm wondering how to remove old containers. The docker rm 3e552code34a
lets you remove a single one, but I have lots already. docker rm --help
doesn't give a selection option (like all, or by image name).
Maybe there is a directory in which these containers are stored where I can delete them easily manually?
docker
docker
edited May 23 '17 at 11:55
Community♦
11
11
asked Jun 21 '13 at 13:41
qkrijgerqkrijger
15.2k62734
15.2k62734
21
You should also consider cleaning orphaned docker volumes. I often find that they consume much more space than old containers and old images. Good script for removing orphaned docker volumes is available at: github.com/chadoe/docker-cleanup-volumes.
– Nemanja Trifunovic
Dec 1 '15 at 18:06
Maybe github.com/chadoe/docker-cleanup-volumes can help you.
– user1929959
Sep 13 '16 at 19:34
You can also usedocker run
with the--rm
flag which would make the container ephemeral, removing all container files after the run.
– Gordon
Sep 21 '16 at 5:51
12
With docker 1.13 (Q4 2016), you can also consider the newdocker system prune
command. See my answer below.
– VonC
Oct 4 '16 at 19:52
1
Use the docker management toolPortainer
We can manageall the old containers, non using volumes and images
by using this tool Its a simple management UI for dockersPlease refer my update below on how to deploy the application
– Anish Varghese
Nov 22 '18 at 4:47
|
show 3 more comments
21
You should also consider cleaning orphaned docker volumes. I often find that they consume much more space than old containers and old images. Good script for removing orphaned docker volumes is available at: github.com/chadoe/docker-cleanup-volumes.
– Nemanja Trifunovic
Dec 1 '15 at 18:06
Maybe github.com/chadoe/docker-cleanup-volumes can help you.
– user1929959
Sep 13 '16 at 19:34
You can also usedocker run
with the--rm
flag which would make the container ephemeral, removing all container files after the run.
– Gordon
Sep 21 '16 at 5:51
12
With docker 1.13 (Q4 2016), you can also consider the newdocker system prune
command. See my answer below.
– VonC
Oct 4 '16 at 19:52
1
Use the docker management toolPortainer
We can manageall the old containers, non using volumes and images
by using this tool Its a simple management UI for dockersPlease refer my update below on how to deploy the application
– Anish Varghese
Nov 22 '18 at 4:47
21
21
You should also consider cleaning orphaned docker volumes. I often find that they consume much more space than old containers and old images. Good script for removing orphaned docker volumes is available at: github.com/chadoe/docker-cleanup-volumes.
– Nemanja Trifunovic
Dec 1 '15 at 18:06
You should also consider cleaning orphaned docker volumes. I often find that they consume much more space than old containers and old images. Good script for removing orphaned docker volumes is available at: github.com/chadoe/docker-cleanup-volumes.
– Nemanja Trifunovic
Dec 1 '15 at 18:06
Maybe github.com/chadoe/docker-cleanup-volumes can help you.
– user1929959
Sep 13 '16 at 19:34
Maybe github.com/chadoe/docker-cleanup-volumes can help you.
– user1929959
Sep 13 '16 at 19:34
You can also use
docker run
with the --rm
flag which would make the container ephemeral, removing all container files after the run.– Gordon
Sep 21 '16 at 5:51
You can also use
docker run
with the --rm
flag which would make the container ephemeral, removing all container files after the run.– Gordon
Sep 21 '16 at 5:51
12
12
With docker 1.13 (Q4 2016), you can also consider the new
docker system prune
command. See my answer below.– VonC
Oct 4 '16 at 19:52
With docker 1.13 (Q4 2016), you can also consider the new
docker system prune
command. See my answer below.– VonC
Oct 4 '16 at 19:52
1
1
Use the docker management tool
Portainer
We can manageall the old containers, non using volumes and images
by using this tool Its a simple management UI for dockers Please refer my update below on how to deploy the application
– Anish Varghese
Nov 22 '18 at 4:47
Use the docker management tool
Portainer
We can manageall the old containers, non using volumes and images
by using this tool Its a simple management UI for dockers Please refer my update below on how to deploy the application
– Anish Varghese
Nov 22 '18 at 4:47
|
show 3 more comments
53 Answers
53
active
oldest
votes
1 2
next
There is a new feature in Docker 1.13.x called Docker container prune:
docker container prune
This will do what you want and should work on all platforms the same way.
There is also a Docker system prune: docker system prune
, which will clean up containers, images, volumes, and networks all in one command.
Original Answer:
There has been some talk about a Docker cleanup command. You can find the information on this ticket: Implement a 'clean' command (#928)
Until that command is available, you can string Docker commands together with other Unix commands to get what you need. Here is an example on how to clean up old containers that are weeks old:
$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm
To give credit, where it is due, this example is from https://twitter.com/jpetazzo/status/347431091415703552.
90
Similar command to remove all untagged images:docker images | grep "<none>" | awk '{print $3}' | xargs docker rmi
– Mohan Raj
Sep 20 '13 at 21:29
7
@MichelMüller when building an image from a dockerfile you can specify the-rm
option to remove intermediate containers after a successful build.
– Freek Kalter
Nov 13 '13 at 20:30
3
@MichelMüller the images are what are used for caching, so removing the containers won't effect that.
– Ken Cochrane
Nov 13 '13 at 21:34
7
If you want to use awk for this consider this command if you want do rm all stopped containers (without a error because of the first line):docker ps -a | awk 'NR > 1 {print $1}' | xargs docker rm
– masi
Jun 15 '14 at 14:45
2
If you get permission denied errors, addsudo
before docker on the last command:$ sudo docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
– Luciano Bargmann
Nov 12 '14 at 17:33
|
show 16 more comments
Another method, which I got from Guillaume J. Charmes (credit where it is due):
docker rm `docker ps --no-trunc -aq`
will remove all containers in an elegant way.
And by Bartosz Bilicki, for Windows:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
For PowerShell:
docker rm @(docker ps -aq)
An update with Docker 1.13 (Q4 2016), credit to VonC (later in this thread):
docker system prune
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
11
this somehow doesnt work for me, butdocker ps -a -q | xargs docker rm
works, (sudo may be needed)
– FUD
Jan 9 '14 at 9:29
84
what aboutdocker rm $(docker ps -a -q)
?
– qkrijger
Jan 9 '14 at 20:48
8
@qkrijger, this doesn't change anything from your original answer, except for adding bashism. The problem is with command line overflow. xargs variant deals with it in a nice way. I would also add --no-trunk to docker ps to avoid (unlikely) id clashes.
– Ihor Kaharlichenko
Feb 7 '14 at 22:32
8
be aware if you use data-only container, it will remove them also
– nXqd
Mar 26 '14 at 12:08
4
I don't see the point with--no-trunc
sincedocker rm
doesn't need full IDs anyway.
– Pithikos
Aug 8 '14 at 9:44
|
show 15 more comments
Updated Answer
Use docker system prune
or docker container prune
now. See VonC's updated answer.
Previous Answer
Composing several different hints above, the most elegant way to remove all non-running containers seems to be:
docker rm $(docker ps -q -f status=exited)
-q
prints just the container ids (without column headers)
-f
allows you to filter your list of printed containers (in this case we are filtering to only show exited containers)
7
This should be the correct answer because it relies on docker to tell you which containers are no longer used, rather than assuming the text output format of $(docker images) will not change. Bonus points for spawning only one extra process instead of two.
– eurythmia
Aug 26 '15 at 11:43
18
You might want to add-v
to avoid dangling volumes:docker rm -v $(docker ps -q -f status=exited)
. See docs.docker.com/userguide/dockervolumes/… for details about dangling volumes.
– rluba
Oct 16 '15 at 13:19
@rluba apparently in Docker 1.9 there will be a separate volume api for handling that problem. github.com/docker/docker/pull/14242
– Ryan Walls
Oct 16 '15 at 16:47
an alias might be handy:alias docker-gc="docker rm -v $(docker ps -q -f status=exited)"
– Frank Neblung
Jul 15 '16 at 9:36
This also works under Windows with PowerShell.
– Tobias
Dec 20 '16 at 14:21
|
show 2 more comments
The official way is:
docker rm `docker ps -aq`
The Docker maintainers have indicated there will be no command for this - and you compose the commands like that:
We have discussed this before and prefer users to use the above line without having to add additional code to Docker.
6
This is a bad solution though, because it'll also delete storage containers.
– Sobrique
Jan 10 '16 at 16:39
8
What do you mean "storage containers"? If you mean data-only containers which once upon a time were used to keep references to data volumes, those are no longer necessary because of the availability ofdocker volume
subcommand, and named volumes.
– L0j1k
Apr 1 '16 at 23:52
Note that this won't work inside Windows Powershell. For that, this is a better solution: stackoverflow.com/a/23674294/1150683
– Bram Vanroy
Jan 30 '18 at 9:53
@Sobrique If you don't react, at least you should delete your comment, it is nearly complete and harmful nonsense.
– peterh
Feb 25 '18 at 16:22
add a comment |
With Docker 1.13 (Q4 2016), you now have:
docker system prune -a
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
docker system prune
without -a will remove (for images) only dangling images, or images without a tag, as commented by smilebomb.
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune -a
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
As wjv comments,
There is also
docker {container,image,volume,network} prune
, which may be used to remove unused instances of just one type of object.
Introduced in commit 913e5cb, only for Docker 1.13+.
docker container prune
Thanks for sharing - for this to become visible, I'm quoting you in the answer high up in the thread
– qkrijger
Oct 6 '16 at 18:43
I upvoted you already :)
– qkrijger
Oct 7 '16 at 11:56
As usual, add the-f
flag to force the cleanup with no questions asked.
– Dirk
Feb 3 '17 at 11:21
2
should be marked as the nowdays correct answer, most of the above were right back in time, but are nodays overcomplicated
– Eugen Mayer
Jun 25 '17 at 10:59
Agreed, so much easier lately, up up you go!
– Maxim Geerinck
Jul 14 '17 at 6:44
|
show 4 more comments
It is now possible to use filtering with docker ps
:
docker rm $(docker ps -q -f status=exited)
And for images:
docker rmi $(docker images -q -f "dangling=true")
However, any of those will cause docker rm
or docker rmi
to throw an error when there are no matching containers. The older docker rm $(docker ps -aq)
trick was even worse as it tried to remove any running container, failing at each one.
Here's a cleaner script to add in your ~/.bashrc
or ~/.profile
:
# Use `docker-cleanup --dry-run` to see what would be deleted.
function docker-cleanup {
EXITED=$(docker ps -q -f status=exited)
DANGLING=$(docker images -q -f "dangling=true")
if [ "$1" == "--dry-run" ]; then
echo "==> Would stop containers:"
echo $EXITED
echo "==> And images:"
echo $DANGLING
else
if [ -n "$EXITED" ]; then
docker rm $EXITED
else
echo "No containers to remove."
fi
if [ -n "$DANGLING" ]; then
docker rmi $DANGLING
else
echo "No images to remove."
fi
fi
}
Edit: As noted below, original answer was for removing images, not containers. Updated to answer both, including new links to documentation. Thanks to Adrian (and Ryan's answer) for mentioning the new ps
filtering.
2
Note the real avantage of this - you don't accidentally kill offstorage
conatiners.
– Sobrique
Jan 10 '16 at 16:41
2
You can also usestatus=created
to get ones that are not running and have never run.
– enderland
Feb 17 '16 at 15:41
add a comment |
UPDATED 2017 (NEWEST)
docker container prune
This - 2017 (OLD) way
To remove ALL STOPPED CONTAINERS
docker rm $(docker ps -a -q)
To remove ALL CONTAINERS (STOPPED AND NON STOPPED)
docker rm -f $(docker ps -a -q)
worked for me on OS X
– cljk
Jan 30 '17 at 19:03
3
downvoted, since this is for sure not the way you do that in 2017 - whis would bedocker volume/container/image prune
, see stackoverflow.com/a/39860665/3625317
– Eugen Mayer
Jun 25 '17 at 11:00
4
upvoted to counter the downvote because a) this answer was from 2016, and b) plenty of folks dont run the bleeding edge.
– keen
Oct 2 '17 at 20:58
add a comment |
Remove all stopped containers:
docker rm $(docker ps -a | grep Exited | awk '{print $1}')
From the comment by pauk960:
Since version 1.3.0 you can use filters with
docker ps
, instead ofgrep Exited
usedocker ps -a -f status=exited
. And if you use-q
with it you can get container IDs only instead of full output, no need to use awk for that.
add a comment |
If you do not like to remove all containers, you can select all containers created before or after a specific container with docker ps --before <container-ID>
or with docker ps --since <container-ID>
. This feature is at least in Docker version 0.6.5.
Let's say you have developed your system, and now it is working, but there are a number of containers left. You want to remove containers created before that working version. Determine the ID of the working container with docker ps
.
Remove containers created before an other container
docker rm $(docker ps --before 9c49c11c8d21 -q)
Another example. You have your database already running on a Docker container. You have developed your application to run on another container and now you have a number of unneeded containers.
Remove containers created after a certain container
docker rm $(docker ps --since a6ca4661ec7f -q)
Docker stores containers in /var/lib/docker/containers
in Ubuntu. I think extra containers do no other harm, but take up disk space.
add a comment |
Update: As of Docker version 1.13 (released January 2017), you can issue the following command to clean up stopped containers, unused volumes, dangling images and unused networks:
docker system prune
If you want to insure that you're only deleting containers which have an exited
status, use this:
docker ps -aq -f status=exited | xargs docker rm
Similarly, if you're cleaning up docker stuff, you can get rid of untagged, unnamed images in this way:
docker images -q --no-trunc -f dangling=true | xargs docker rmi
add a comment |
Here is my docker-cleanup
script, which removes untagged containers and images. Please check the source for any updates.
#!/bin/sh
# Cleanup docker files: untagged containers and images.
#
# Use `docker-cleanup -n` for a dry run to see what would be deleted.
untagged_containers() {
# Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
docker ps -a | awk '$2 ~ "[0-9a-f]{12}" {print $'$1'}'
}
untagged_images() {
# Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
# NOTE: intermediate images (via -a) seem to only cause
# "Error: Conflict, foobarid wasn't deleted" messages.
# Might be useful sometimes when Docker messed things up?!
# docker images -a | awk '$1 == "<none>" {print $'$1'}'
docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
}
# Dry-run.
if [ "$1" = "-n" ]; then
echo "=== Containers with uncommitted images: ==="
untagged_containers 0
echo
echo "=== Uncommitted images: ==="
untagged_images 0
exit
fi
# Remove containers with untagged images.
echo "Removing containers:" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
# Remove untagged images
echo "Removing images:" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi
Source: https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup
@BradMurray Thanks! You might also like these: github.com/blueyed/dotfiles/commit/…
– blueyed
Jun 7 '14 at 23:06
3
Does not work on OSX because of thexargs --no-run-if-empty
option
– caktux
Jul 14 '15 at 5:36
add a comment |
First, stop running containers before attempting to remove them
Remove running containers
docker rm $(docker stop -t=1 $(docker ps -q))
You could use kill
instead of stop
. In my case I prefer stop
since I tend to rerun them vs. creating a new one every time so I try to shut them down nicely.
Note: Trying to stop a container will give you an error:
Error: Impossible to remove a running container, please stop it first
Remove all containers
docker rm $(docker ps -a -q)
add a comment |
Removing all containers from Windows shell:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
I'll include this in my answer since that is first page. Thanks
– qkrijger
Apr 17 '16 at 10:58
"from Windows shell"? Do you mean "using the Windows shell (CMD)"?
– Peter Mortensen
Jul 23 '18 at 17:39
add a comment |
https://github.com/HardySimpson/docker-cleanup
Docker cleanup
A tiny all-in-one shell, which removes:
- Containers that not running more than one day ago
- Images that don't belong to any remaining container
Intend to run as a crontab job
Feature
- It will remove all
<none>:<none>
images - If the image has multiple repo:tag references to it, it will remove all repo:tag except with running a container. Actually it is a nature of "docker rmi".
- Many error message will be show on screen, and you can decide to
2>/dev/null
or not - Learn something from docker-gc, and fix its problem (it can not remove image that has mutliple repo:tag)
add a comment |
Use:
docker rm -f $(docker ps -a -q)
It forcefully stops and removes all containers present locally.
Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge.
– Brian Tompsett - 汤莱恩
Sep 7 '16 at 14:25
In ubuntu 16.04, docker requires sudo, so you have to writesudo docker rm -f $(sudo docker ps -a -q)
.
– Elliott
Sep 21 '16 at 15:47
as a plus, this also works in Windows under powershell!
– Robert Ivanc
Oct 21 '16 at 14:34
@Elliott I thinksudo
is required everywhere unless you add your user to the 'docker' group. I use ubuntu 16.10 without sudo by adding my user to the docker group.
– emory
Mar 26 '17 at 11:38
add a comment |
Remove all stopped containers.
sudo docker rm $(sudo docker ps -a -q)
This will remove all stopped containers by getting a list of all containers with docker ps -a -q and passing their ids to docker rm. This should not remove any running containers, and it will tell you it can’t remove a running image.
Remove all untagged images
Now you want to clean up old images to save some space.
sudo docker rmi $(sudo docker images -q --filter "dangling=true")
Don't forget to delete docker volumes by specifying the -v option (sudo docker rm -v $(sudo docker ps -a -q))
– vcarel
Nov 9 '15 at 10:30
Didn't look this up, but I guess the same as:docker ps -aq | xargs docker rm -v
– quine
Mar 23 '18 at 20:24
This only works on Linux, not Windows... Butdocker rm -f $(docker ps -a -q)
works in PowerShell / Docker for Windows.
– Peter Mortensen
Jul 24 '18 at 7:58
add a comment |
So, personally I recommend doing this as part of your deploy script for both images and containers, keeping only the most recent n containers and images. I tag my Docker images with the same versioning schema I use with git tag
as well as always tagging the latest Docker image with "latest." This means that without cleaning up anything, my Docker images wind up looking like:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
some_repo/some_image 0.0.5 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image latest 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image 0.0.4 0beabfa514ea 45 hours ago 925.4 MB
some_repo/some_image 0.0.3 54302cd10bf2 6 days ago 978.5 MB
some_repo/some_image 0.0.2 0078b30f3d9a 7 days ago 978.5 MB
some_repo/some_image 0.0.1 sdfgdf0f3d9a 8 days ago 938.5 MB
Now, of course I don't want to keep all my images (or containers) going back to perpetuity on all my production boxes. I just want the last 3 or 4 for rollbacks and to get rid of everything else. Unix's tail
is your best friend here. Since docker images
and docker ps
both order by date, we can just use tail
to select all but the top three and remove them:
docker rmi $(docker images -q | tail -n +4)
Run that along with your deploy scripts (or locally) to always keep just enough images to comfortably roll back without taking up too much room or cluttering stuff up with old images.
Personally, I only keep one container on my production box at any time, but you can do the same sort of thing with containers if you want more:
docker rm $(docker ps -aq | tail -n +4)
Finally, in my simplified example we're only dealing with one repository at a time, but if you had more, you can just get a bit more sophisticated with the same idea. Say I just want to keep the last three images from some_repo/some_image. I can just mix in grep
and awk
and be on my way:
docker rmi $(docker images -a | grep 'some_repo/some_image' | awk '{print $3}' | tail -n +4)
Again, the same idea applies to containers, but you get it by this point so I'll stop giving examples.
add a comment |
Remove 5 oldest containers:
docker rm `docker ps -aq | tail -n 5`
See how many containers there are left:
docker ps -aq | wc -l
add a comment |
New way:
spotify/docker-gc play the trick.
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc spotify/docker-gc
- Containers that exited more than an hour ago are removed.
- Images that don't belong to any remaining container after that are removed
It has supported environmental settings
Forcing deletion of images that have multiple tags
FORCE_IMAGE_REMOVAL=1
Forcing deletion of containers
FORCE_CONTAINER_REMOVAL=1
Excluding Recently Exited Containers and Images From Garbage Collection
GRACE_PERIOD_SECONDS=86400
This setting also prevents the removal of images that have been created less than GRACE_PERIOD_SECONDS seconds ago.
Dry run
DRY_RUN=1
Cleaning up orphaned container volumes
CLEAN_UP_VOLUMES=1
Reference: docker-gc
Old way to do:
delete old, non-running containers
docker ps -a -q -f status=exited | xargs --no-run-if-empty docker rm
OR
docker rm $(docker ps -a -q)
delete all images associated with non-running docker containers
docker images -q | xargs --no-run-if-empty docker rmi
cleanup orphaned docker volumes for docker version 1.10.x and above
docker volume ls -qf dangling=true | xargs -r docker volume rm
Based on time period
docker ps -a | grep "weeks ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "days ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "hours ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
1
Interesting alternative. +1
– VonC
Nov 20 '16 at 12:26
add a comment |
Remove all docker processes:
docker rm $(docker ps -a -q)
Remove specific container:
$ docker ps -a (lists all old containers)
$ docker rm container-Id
add a comment |
You can use the following command to remove the exited containers:
docker rm $(sudo docker ps -a | grep Exit | cut -d ' ' -f 1)
Here is the full gist to also remove the old images on docker: Gist to remove old Docker containers and images.
Practically a duplicate stackoverflow.com/a/24681946/1938621
– Ryne Everett
Jan 4 '15 at 2:09
add a comment |
The basic steps to stop/remove all containers and images
List all the containers
docker ps -aq
Stop all running containers
docker stop $(docker ps -aq)
Remove all containers
docker rm $(docker ps -aq)
Remove all images
docker rmi $(docker images -q)
Note: First you have to stop all the running containers before you remove them. Also before removing an image, you have to stop and remove its dependent container(s).
add a comment |
#!/bin/bash
# docker-gc --- Remove stopped docker containers
RUNNING=$(docker ps -q)
ALL=$(docker ps -a -q)
for container in $ALL ; do
[[ "$RUNNING" =~ "$container" ]] && continue
echo Removing container: $(docker rm $container)
done
for those that don't want to search for it themselves github.com/spotify/docker-gc
– grim
Feb 24 '15 at 19:13
add a comment |
I always use docker rmi $(docker ps -a -q)
to remove all images.
You can remove directory /var/lib/docker/graph
when docker rmi
failed.
Thanks for adding the /var/lib/docker/graph info
– Jonathan DeMarks
Mar 5 '15 at 15:26
The questions was how to remove containers, not images.
– pauk960
Mar 16 '15 at 19:34
add a comment |
You can remove only stopped containers. Stop all of them in the beginning
docker stop $(docker ps -a -q)
Then you can remove
docker rm $(docker ps -a -q)
add a comment |
Try this command to clean containers and dangling images.
docker system prune -f
This is an amazing pro-tip!
– b01
Jun 15 '18 at 18:05
add a comment |
To remove ALL containers without crying:
sudo docker ps -a | grep -v CONTAINER | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
add a comment |
For anyone interested, I took the example from qkrijger and turned it into a clear all (stop and remove all)
docker stop `docker ps --no-trunc -aq` ; docker rm `docker ps --no-trunc -aq`
If you are removing the containers anyway, thendocker rm -f $(docker ps --no-trunc -aq)
is faster
– qkrijger
Apr 10 '15 at 7:50
True, but if you need it to go through proper shutdown to cleanup shared or reused resources (i.e. Attached volumes, correctly closed active connections, etc) then you need to call stop first. Great example, if you have a Jenkins container, then killing the container is a bad idea. You want it to save any unpersisted changes, warn logged in users, and, very importantly, properly stop all builds.
– Kraig McConaghy
Apr 10 '15 at 12:57
agreed, that is a very valid use case
– qkrijger
Apr 26 '15 at 22:55
add a comment |
docker rm --force `docker ps -qa`
add a comment |
Remove all containers created from a certain image:
docker rm $(docker ps -a | awk '/myimage:mytag/{print $1}')
Nice, I like it :D
– qkrijger
May 20 '14 at 8:37
add a comment |
1 2
next
protected by Baum mit Augen Aug 25 '17 at 20:22
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
53 Answers
53
active
oldest
votes
53 Answers
53
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 2
next
There is a new feature in Docker 1.13.x called Docker container prune:
docker container prune
This will do what you want and should work on all platforms the same way.
There is also a Docker system prune: docker system prune
, which will clean up containers, images, volumes, and networks all in one command.
Original Answer:
There has been some talk about a Docker cleanup command. You can find the information on this ticket: Implement a 'clean' command (#928)
Until that command is available, you can string Docker commands together with other Unix commands to get what you need. Here is an example on how to clean up old containers that are weeks old:
$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm
To give credit, where it is due, this example is from https://twitter.com/jpetazzo/status/347431091415703552.
90
Similar command to remove all untagged images:docker images | grep "<none>" | awk '{print $3}' | xargs docker rmi
– Mohan Raj
Sep 20 '13 at 21:29
7
@MichelMüller when building an image from a dockerfile you can specify the-rm
option to remove intermediate containers after a successful build.
– Freek Kalter
Nov 13 '13 at 20:30
3
@MichelMüller the images are what are used for caching, so removing the containers won't effect that.
– Ken Cochrane
Nov 13 '13 at 21:34
7
If you want to use awk for this consider this command if you want do rm all stopped containers (without a error because of the first line):docker ps -a | awk 'NR > 1 {print $1}' | xargs docker rm
– masi
Jun 15 '14 at 14:45
2
If you get permission denied errors, addsudo
before docker on the last command:$ sudo docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
– Luciano Bargmann
Nov 12 '14 at 17:33
|
show 16 more comments
There is a new feature in Docker 1.13.x called Docker container prune:
docker container prune
This will do what you want and should work on all platforms the same way.
There is also a Docker system prune: docker system prune
, which will clean up containers, images, volumes, and networks all in one command.
Original Answer:
There has been some talk about a Docker cleanup command. You can find the information on this ticket: Implement a 'clean' command (#928)
Until that command is available, you can string Docker commands together with other Unix commands to get what you need. Here is an example on how to clean up old containers that are weeks old:
$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm
To give credit, where it is due, this example is from https://twitter.com/jpetazzo/status/347431091415703552.
90
Similar command to remove all untagged images:docker images | grep "<none>" | awk '{print $3}' | xargs docker rmi
– Mohan Raj
Sep 20 '13 at 21:29
7
@MichelMüller when building an image from a dockerfile you can specify the-rm
option to remove intermediate containers after a successful build.
– Freek Kalter
Nov 13 '13 at 20:30
3
@MichelMüller the images are what are used for caching, so removing the containers won't effect that.
– Ken Cochrane
Nov 13 '13 at 21:34
7
If you want to use awk for this consider this command if you want do rm all stopped containers (without a error because of the first line):docker ps -a | awk 'NR > 1 {print $1}' | xargs docker rm
– masi
Jun 15 '14 at 14:45
2
If you get permission denied errors, addsudo
before docker on the last command:$ sudo docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
– Luciano Bargmann
Nov 12 '14 at 17:33
|
show 16 more comments
There is a new feature in Docker 1.13.x called Docker container prune:
docker container prune
This will do what you want and should work on all platforms the same way.
There is also a Docker system prune: docker system prune
, which will clean up containers, images, volumes, and networks all in one command.
Original Answer:
There has been some talk about a Docker cleanup command. You can find the information on this ticket: Implement a 'clean' command (#928)
Until that command is available, you can string Docker commands together with other Unix commands to get what you need. Here is an example on how to clean up old containers that are weeks old:
$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm
To give credit, where it is due, this example is from https://twitter.com/jpetazzo/status/347431091415703552.
There is a new feature in Docker 1.13.x called Docker container prune:
docker container prune
This will do what you want and should work on all platforms the same way.
There is also a Docker system prune: docker system prune
, which will clean up containers, images, volumes, and networks all in one command.
Original Answer:
There has been some talk about a Docker cleanup command. You can find the information on this ticket: Implement a 'clean' command (#928)
Until that command is available, you can string Docker commands together with other Unix commands to get what you need. Here is an example on how to clean up old containers that are weeks old:
$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm
To give credit, where it is due, this example is from https://twitter.com/jpetazzo/status/347431091415703552.
edited Jul 31 '18 at 13:17
David Birks
4915
4915
answered Jun 21 '13 at 14:25
Ken CochraneKen Cochrane
54.8k74254
54.8k74254
90
Similar command to remove all untagged images:docker images | grep "<none>" | awk '{print $3}' | xargs docker rmi
– Mohan Raj
Sep 20 '13 at 21:29
7
@MichelMüller when building an image from a dockerfile you can specify the-rm
option to remove intermediate containers after a successful build.
– Freek Kalter
Nov 13 '13 at 20:30
3
@MichelMüller the images are what are used for caching, so removing the containers won't effect that.
– Ken Cochrane
Nov 13 '13 at 21:34
7
If you want to use awk for this consider this command if you want do rm all stopped containers (without a error because of the first line):docker ps -a | awk 'NR > 1 {print $1}' | xargs docker rm
– masi
Jun 15 '14 at 14:45
2
If you get permission denied errors, addsudo
before docker on the last command:$ sudo docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
– Luciano Bargmann
Nov 12 '14 at 17:33
|
show 16 more comments
90
Similar command to remove all untagged images:docker images | grep "<none>" | awk '{print $3}' | xargs docker rmi
– Mohan Raj
Sep 20 '13 at 21:29
7
@MichelMüller when building an image from a dockerfile you can specify the-rm
option to remove intermediate containers after a successful build.
– Freek Kalter
Nov 13 '13 at 20:30
3
@MichelMüller the images are what are used for caching, so removing the containers won't effect that.
– Ken Cochrane
Nov 13 '13 at 21:34
7
If you want to use awk for this consider this command if you want do rm all stopped containers (without a error because of the first line):docker ps -a | awk 'NR > 1 {print $1}' | xargs docker rm
– masi
Jun 15 '14 at 14:45
2
If you get permission denied errors, addsudo
before docker on the last command:$ sudo docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
– Luciano Bargmann
Nov 12 '14 at 17:33
90
90
Similar command to remove all untagged images:
docker images | grep "<none>" | awk '{print $3}' | xargs docker rmi
– Mohan Raj
Sep 20 '13 at 21:29
Similar command to remove all untagged images:
docker images | grep "<none>" | awk '{print $3}' | xargs docker rmi
– Mohan Raj
Sep 20 '13 at 21:29
7
7
@MichelMüller when building an image from a dockerfile you can specify the
-rm
option to remove intermediate containers after a successful build.– Freek Kalter
Nov 13 '13 at 20:30
@MichelMüller when building an image from a dockerfile you can specify the
-rm
option to remove intermediate containers after a successful build.– Freek Kalter
Nov 13 '13 at 20:30
3
3
@MichelMüller the images are what are used for caching, so removing the containers won't effect that.
– Ken Cochrane
Nov 13 '13 at 21:34
@MichelMüller the images are what are used for caching, so removing the containers won't effect that.
– Ken Cochrane
Nov 13 '13 at 21:34
7
7
If you want to use awk for this consider this command if you want do rm all stopped containers (without a error because of the first line):
docker ps -a | awk 'NR > 1 {print $1}' | xargs docker rm
– masi
Jun 15 '14 at 14:45
If you want to use awk for this consider this command if you want do rm all stopped containers (without a error because of the first line):
docker ps -a | awk 'NR > 1 {print $1}' | xargs docker rm
– masi
Jun 15 '14 at 14:45
2
2
If you get permission denied errors, add
sudo
before docker on the last command: $ sudo docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
– Luciano Bargmann
Nov 12 '14 at 17:33
If you get permission denied errors, add
sudo
before docker on the last command: $ sudo docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
– Luciano Bargmann
Nov 12 '14 at 17:33
|
show 16 more comments
Another method, which I got from Guillaume J. Charmes (credit where it is due):
docker rm `docker ps --no-trunc -aq`
will remove all containers in an elegant way.
And by Bartosz Bilicki, for Windows:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
For PowerShell:
docker rm @(docker ps -aq)
An update with Docker 1.13 (Q4 2016), credit to VonC (later in this thread):
docker system prune
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
11
this somehow doesnt work for me, butdocker ps -a -q | xargs docker rm
works, (sudo may be needed)
– FUD
Jan 9 '14 at 9:29
84
what aboutdocker rm $(docker ps -a -q)
?
– qkrijger
Jan 9 '14 at 20:48
8
@qkrijger, this doesn't change anything from your original answer, except for adding bashism. The problem is with command line overflow. xargs variant deals with it in a nice way. I would also add --no-trunk to docker ps to avoid (unlikely) id clashes.
– Ihor Kaharlichenko
Feb 7 '14 at 22:32
8
be aware if you use data-only container, it will remove them also
– nXqd
Mar 26 '14 at 12:08
4
I don't see the point with--no-trunc
sincedocker rm
doesn't need full IDs anyway.
– Pithikos
Aug 8 '14 at 9:44
|
show 15 more comments
Another method, which I got from Guillaume J. Charmes (credit where it is due):
docker rm `docker ps --no-trunc -aq`
will remove all containers in an elegant way.
And by Bartosz Bilicki, for Windows:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
For PowerShell:
docker rm @(docker ps -aq)
An update with Docker 1.13 (Q4 2016), credit to VonC (later in this thread):
docker system prune
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
11
this somehow doesnt work for me, butdocker ps -a -q | xargs docker rm
works, (sudo may be needed)
– FUD
Jan 9 '14 at 9:29
84
what aboutdocker rm $(docker ps -a -q)
?
– qkrijger
Jan 9 '14 at 20:48
8
@qkrijger, this doesn't change anything from your original answer, except for adding bashism. The problem is with command line overflow. xargs variant deals with it in a nice way. I would also add --no-trunk to docker ps to avoid (unlikely) id clashes.
– Ihor Kaharlichenko
Feb 7 '14 at 22:32
8
be aware if you use data-only container, it will remove them also
– nXqd
Mar 26 '14 at 12:08
4
I don't see the point with--no-trunc
sincedocker rm
doesn't need full IDs anyway.
– Pithikos
Aug 8 '14 at 9:44
|
show 15 more comments
Another method, which I got from Guillaume J. Charmes (credit where it is due):
docker rm `docker ps --no-trunc -aq`
will remove all containers in an elegant way.
And by Bartosz Bilicki, for Windows:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
For PowerShell:
docker rm @(docker ps -aq)
An update with Docker 1.13 (Q4 2016), credit to VonC (later in this thread):
docker system prune
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
Another method, which I got from Guillaume J. Charmes (credit where it is due):
docker rm `docker ps --no-trunc -aq`
will remove all containers in an elegant way.
And by Bartosz Bilicki, for Windows:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
For PowerShell:
docker rm @(docker ps -aq)
An update with Docker 1.13 (Q4 2016), credit to VonC (later in this thread):
docker system prune
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
edited May 23 '17 at 12:10
Community♦
11
11
answered Aug 9 '13 at 6:41
qkrijgerqkrijger
15.2k62734
15.2k62734
11
this somehow doesnt work for me, butdocker ps -a -q | xargs docker rm
works, (sudo may be needed)
– FUD
Jan 9 '14 at 9:29
84
what aboutdocker rm $(docker ps -a -q)
?
– qkrijger
Jan 9 '14 at 20:48
8
@qkrijger, this doesn't change anything from your original answer, except for adding bashism. The problem is with command line overflow. xargs variant deals with it in a nice way. I would also add --no-trunk to docker ps to avoid (unlikely) id clashes.
– Ihor Kaharlichenko
Feb 7 '14 at 22:32
8
be aware if you use data-only container, it will remove them also
– nXqd
Mar 26 '14 at 12:08
4
I don't see the point with--no-trunc
sincedocker rm
doesn't need full IDs anyway.
– Pithikos
Aug 8 '14 at 9:44
|
show 15 more comments
11
this somehow doesnt work for me, butdocker ps -a -q | xargs docker rm
works, (sudo may be needed)
– FUD
Jan 9 '14 at 9:29
84
what aboutdocker rm $(docker ps -a -q)
?
– qkrijger
Jan 9 '14 at 20:48
8
@qkrijger, this doesn't change anything from your original answer, except for adding bashism. The problem is with command line overflow. xargs variant deals with it in a nice way. I would also add --no-trunk to docker ps to avoid (unlikely) id clashes.
– Ihor Kaharlichenko
Feb 7 '14 at 22:32
8
be aware if you use data-only container, it will remove them also
– nXqd
Mar 26 '14 at 12:08
4
I don't see the point with--no-trunc
sincedocker rm
doesn't need full IDs anyway.
– Pithikos
Aug 8 '14 at 9:44
11
11
this somehow doesnt work for me, but
docker ps -a -q | xargs docker rm
works, (sudo may be needed)– FUD
Jan 9 '14 at 9:29
this somehow doesnt work for me, but
docker ps -a -q | xargs docker rm
works, (sudo may be needed)– FUD
Jan 9 '14 at 9:29
84
84
what about
docker rm $(docker ps -a -q)
?– qkrijger
Jan 9 '14 at 20:48
what about
docker rm $(docker ps -a -q)
?– qkrijger
Jan 9 '14 at 20:48
8
8
@qkrijger, this doesn't change anything from your original answer, except for adding bashism. The problem is with command line overflow. xargs variant deals with it in a nice way. I would also add --no-trunk to docker ps to avoid (unlikely) id clashes.
– Ihor Kaharlichenko
Feb 7 '14 at 22:32
@qkrijger, this doesn't change anything from your original answer, except for adding bashism. The problem is with command line overflow. xargs variant deals with it in a nice way. I would also add --no-trunk to docker ps to avoid (unlikely) id clashes.
– Ihor Kaharlichenko
Feb 7 '14 at 22:32
8
8
be aware if you use data-only container, it will remove them also
– nXqd
Mar 26 '14 at 12:08
be aware if you use data-only container, it will remove them also
– nXqd
Mar 26 '14 at 12:08
4
4
I don't see the point with
--no-trunc
since docker rm
doesn't need full IDs anyway.– Pithikos
Aug 8 '14 at 9:44
I don't see the point with
--no-trunc
since docker rm
doesn't need full IDs anyway.– Pithikos
Aug 8 '14 at 9:44
|
show 15 more comments
Updated Answer
Use docker system prune
or docker container prune
now. See VonC's updated answer.
Previous Answer
Composing several different hints above, the most elegant way to remove all non-running containers seems to be:
docker rm $(docker ps -q -f status=exited)
-q
prints just the container ids (without column headers)
-f
allows you to filter your list of printed containers (in this case we are filtering to only show exited containers)
7
This should be the correct answer because it relies on docker to tell you which containers are no longer used, rather than assuming the text output format of $(docker images) will not change. Bonus points for spawning only one extra process instead of two.
– eurythmia
Aug 26 '15 at 11:43
18
You might want to add-v
to avoid dangling volumes:docker rm -v $(docker ps -q -f status=exited)
. See docs.docker.com/userguide/dockervolumes/… for details about dangling volumes.
– rluba
Oct 16 '15 at 13:19
@rluba apparently in Docker 1.9 there will be a separate volume api for handling that problem. github.com/docker/docker/pull/14242
– Ryan Walls
Oct 16 '15 at 16:47
an alias might be handy:alias docker-gc="docker rm -v $(docker ps -q -f status=exited)"
– Frank Neblung
Jul 15 '16 at 9:36
This also works under Windows with PowerShell.
– Tobias
Dec 20 '16 at 14:21
|
show 2 more comments
Updated Answer
Use docker system prune
or docker container prune
now. See VonC's updated answer.
Previous Answer
Composing several different hints above, the most elegant way to remove all non-running containers seems to be:
docker rm $(docker ps -q -f status=exited)
-q
prints just the container ids (without column headers)
-f
allows you to filter your list of printed containers (in this case we are filtering to only show exited containers)
7
This should be the correct answer because it relies on docker to tell you which containers are no longer used, rather than assuming the text output format of $(docker images) will not change. Bonus points for spawning only one extra process instead of two.
– eurythmia
Aug 26 '15 at 11:43
18
You might want to add-v
to avoid dangling volumes:docker rm -v $(docker ps -q -f status=exited)
. See docs.docker.com/userguide/dockervolumes/… for details about dangling volumes.
– rluba
Oct 16 '15 at 13:19
@rluba apparently in Docker 1.9 there will be a separate volume api for handling that problem. github.com/docker/docker/pull/14242
– Ryan Walls
Oct 16 '15 at 16:47
an alias might be handy:alias docker-gc="docker rm -v $(docker ps -q -f status=exited)"
– Frank Neblung
Jul 15 '16 at 9:36
This also works under Windows with PowerShell.
– Tobias
Dec 20 '16 at 14:21
|
show 2 more comments
Updated Answer
Use docker system prune
or docker container prune
now. See VonC's updated answer.
Previous Answer
Composing several different hints above, the most elegant way to remove all non-running containers seems to be:
docker rm $(docker ps -q -f status=exited)
-q
prints just the container ids (without column headers)
-f
allows you to filter your list of printed containers (in this case we are filtering to only show exited containers)
Updated Answer
Use docker system prune
or docker container prune
now. See VonC's updated answer.
Previous Answer
Composing several different hints above, the most elegant way to remove all non-running containers seems to be:
docker rm $(docker ps -q -f status=exited)
-q
prints just the container ids (without column headers)
-f
allows you to filter your list of printed containers (in this case we are filtering to only show exited containers)
edited Jul 23 '18 at 17:32


Peter Mortensen
13.7k1986112
13.7k1986112
answered Apr 6 '15 at 15:34


Ryan WallsRyan Walls
5,88812739
5,88812739
7
This should be the correct answer because it relies on docker to tell you which containers are no longer used, rather than assuming the text output format of $(docker images) will not change. Bonus points for spawning only one extra process instead of two.
– eurythmia
Aug 26 '15 at 11:43
18
You might want to add-v
to avoid dangling volumes:docker rm -v $(docker ps -q -f status=exited)
. See docs.docker.com/userguide/dockervolumes/… for details about dangling volumes.
– rluba
Oct 16 '15 at 13:19
@rluba apparently in Docker 1.9 there will be a separate volume api for handling that problem. github.com/docker/docker/pull/14242
– Ryan Walls
Oct 16 '15 at 16:47
an alias might be handy:alias docker-gc="docker rm -v $(docker ps -q -f status=exited)"
– Frank Neblung
Jul 15 '16 at 9:36
This also works under Windows with PowerShell.
– Tobias
Dec 20 '16 at 14:21
|
show 2 more comments
7
This should be the correct answer because it relies on docker to tell you which containers are no longer used, rather than assuming the text output format of $(docker images) will not change. Bonus points for spawning only one extra process instead of two.
– eurythmia
Aug 26 '15 at 11:43
18
You might want to add-v
to avoid dangling volumes:docker rm -v $(docker ps -q -f status=exited)
. See docs.docker.com/userguide/dockervolumes/… for details about dangling volumes.
– rluba
Oct 16 '15 at 13:19
@rluba apparently in Docker 1.9 there will be a separate volume api for handling that problem. github.com/docker/docker/pull/14242
– Ryan Walls
Oct 16 '15 at 16:47
an alias might be handy:alias docker-gc="docker rm -v $(docker ps -q -f status=exited)"
– Frank Neblung
Jul 15 '16 at 9:36
This also works under Windows with PowerShell.
– Tobias
Dec 20 '16 at 14:21
7
7
This should be the correct answer because it relies on docker to tell you which containers are no longer used, rather than assuming the text output format of $(docker images) will not change. Bonus points for spawning only one extra process instead of two.
– eurythmia
Aug 26 '15 at 11:43
This should be the correct answer because it relies on docker to tell you which containers are no longer used, rather than assuming the text output format of $(docker images) will not change. Bonus points for spawning only one extra process instead of two.
– eurythmia
Aug 26 '15 at 11:43
18
18
You might want to add
-v
to avoid dangling volumes: docker rm -v $(docker ps -q -f status=exited)
. See docs.docker.com/userguide/dockervolumes/… for details about dangling volumes.– rluba
Oct 16 '15 at 13:19
You might want to add
-v
to avoid dangling volumes: docker rm -v $(docker ps -q -f status=exited)
. See docs.docker.com/userguide/dockervolumes/… for details about dangling volumes.– rluba
Oct 16 '15 at 13:19
@rluba apparently in Docker 1.9 there will be a separate volume api for handling that problem. github.com/docker/docker/pull/14242
– Ryan Walls
Oct 16 '15 at 16:47
@rluba apparently in Docker 1.9 there will be a separate volume api for handling that problem. github.com/docker/docker/pull/14242
– Ryan Walls
Oct 16 '15 at 16:47
an alias might be handy:
alias docker-gc="docker rm -v $(docker ps -q -f status=exited)"
– Frank Neblung
Jul 15 '16 at 9:36
an alias might be handy:
alias docker-gc="docker rm -v $(docker ps -q -f status=exited)"
– Frank Neblung
Jul 15 '16 at 9:36
This also works under Windows with PowerShell.
– Tobias
Dec 20 '16 at 14:21
This also works under Windows with PowerShell.
– Tobias
Dec 20 '16 at 14:21
|
show 2 more comments
The official way is:
docker rm `docker ps -aq`
The Docker maintainers have indicated there will be no command for this - and you compose the commands like that:
We have discussed this before and prefer users to use the above line without having to add additional code to Docker.
6
This is a bad solution though, because it'll also delete storage containers.
– Sobrique
Jan 10 '16 at 16:39
8
What do you mean "storage containers"? If you mean data-only containers which once upon a time were used to keep references to data volumes, those are no longer necessary because of the availability ofdocker volume
subcommand, and named volumes.
– L0j1k
Apr 1 '16 at 23:52
Note that this won't work inside Windows Powershell. For that, this is a better solution: stackoverflow.com/a/23674294/1150683
– Bram Vanroy
Jan 30 '18 at 9:53
@Sobrique If you don't react, at least you should delete your comment, it is nearly complete and harmful nonsense.
– peterh
Feb 25 '18 at 16:22
add a comment |
The official way is:
docker rm `docker ps -aq`
The Docker maintainers have indicated there will be no command for this - and you compose the commands like that:
We have discussed this before and prefer users to use the above line without having to add additional code to Docker.
6
This is a bad solution though, because it'll also delete storage containers.
– Sobrique
Jan 10 '16 at 16:39
8
What do you mean "storage containers"? If you mean data-only containers which once upon a time were used to keep references to data volumes, those are no longer necessary because of the availability ofdocker volume
subcommand, and named volumes.
– L0j1k
Apr 1 '16 at 23:52
Note that this won't work inside Windows Powershell. For that, this is a better solution: stackoverflow.com/a/23674294/1150683
– Bram Vanroy
Jan 30 '18 at 9:53
@Sobrique If you don't react, at least you should delete your comment, it is nearly complete and harmful nonsense.
– peterh
Feb 25 '18 at 16:22
add a comment |
The official way is:
docker rm `docker ps -aq`
The Docker maintainers have indicated there will be no command for this - and you compose the commands like that:
We have discussed this before and prefer users to use the above line without having to add additional code to Docker.
The official way is:
docker rm `docker ps -aq`
The Docker maintainers have indicated there will be no command for this - and you compose the commands like that:
We have discussed this before and prefer users to use the above line without having to add additional code to Docker.
edited Mar 10 '17 at 3:11


Peter Mortensen
13.7k1986112
13.7k1986112
answered Jun 18 '14 at 4:23
Michael NealeMichael Neale
13.3k156799
13.3k156799
6
This is a bad solution though, because it'll also delete storage containers.
– Sobrique
Jan 10 '16 at 16:39
8
What do you mean "storage containers"? If you mean data-only containers which once upon a time were used to keep references to data volumes, those are no longer necessary because of the availability ofdocker volume
subcommand, and named volumes.
– L0j1k
Apr 1 '16 at 23:52
Note that this won't work inside Windows Powershell. For that, this is a better solution: stackoverflow.com/a/23674294/1150683
– Bram Vanroy
Jan 30 '18 at 9:53
@Sobrique If you don't react, at least you should delete your comment, it is nearly complete and harmful nonsense.
– peterh
Feb 25 '18 at 16:22
add a comment |
6
This is a bad solution though, because it'll also delete storage containers.
– Sobrique
Jan 10 '16 at 16:39
8
What do you mean "storage containers"? If you mean data-only containers which once upon a time were used to keep references to data volumes, those are no longer necessary because of the availability ofdocker volume
subcommand, and named volumes.
– L0j1k
Apr 1 '16 at 23:52
Note that this won't work inside Windows Powershell. For that, this is a better solution: stackoverflow.com/a/23674294/1150683
– Bram Vanroy
Jan 30 '18 at 9:53
@Sobrique If you don't react, at least you should delete your comment, it is nearly complete and harmful nonsense.
– peterh
Feb 25 '18 at 16:22
6
6
This is a bad solution though, because it'll also delete storage containers.
– Sobrique
Jan 10 '16 at 16:39
This is a bad solution though, because it'll also delete storage containers.
– Sobrique
Jan 10 '16 at 16:39
8
8
What do you mean "storage containers"? If you mean data-only containers which once upon a time were used to keep references to data volumes, those are no longer necessary because of the availability of
docker volume
subcommand, and named volumes.– L0j1k
Apr 1 '16 at 23:52
What do you mean "storage containers"? If you mean data-only containers which once upon a time were used to keep references to data volumes, those are no longer necessary because of the availability of
docker volume
subcommand, and named volumes.– L0j1k
Apr 1 '16 at 23:52
Note that this won't work inside Windows Powershell. For that, this is a better solution: stackoverflow.com/a/23674294/1150683
– Bram Vanroy
Jan 30 '18 at 9:53
Note that this won't work inside Windows Powershell. For that, this is a better solution: stackoverflow.com/a/23674294/1150683
– Bram Vanroy
Jan 30 '18 at 9:53
@Sobrique If you don't react, at least you should delete your comment, it is nearly complete and harmful nonsense.
– peterh
Feb 25 '18 at 16:22
@Sobrique If you don't react, at least you should delete your comment, it is nearly complete and harmful nonsense.
– peterh
Feb 25 '18 at 16:22
add a comment |
With Docker 1.13 (Q4 2016), you now have:
docker system prune -a
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
docker system prune
without -a will remove (for images) only dangling images, or images without a tag, as commented by smilebomb.
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune -a
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
As wjv comments,
There is also
docker {container,image,volume,network} prune
, which may be used to remove unused instances of just one type of object.
Introduced in commit 913e5cb, only for Docker 1.13+.
docker container prune
Thanks for sharing - for this to become visible, I'm quoting you in the answer high up in the thread
– qkrijger
Oct 6 '16 at 18:43
I upvoted you already :)
– qkrijger
Oct 7 '16 at 11:56
As usual, add the-f
flag to force the cleanup with no questions asked.
– Dirk
Feb 3 '17 at 11:21
2
should be marked as the nowdays correct answer, most of the above were right back in time, but are nodays overcomplicated
– Eugen Mayer
Jun 25 '17 at 10:59
Agreed, so much easier lately, up up you go!
– Maxim Geerinck
Jul 14 '17 at 6:44
|
show 4 more comments
With Docker 1.13 (Q4 2016), you now have:
docker system prune -a
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
docker system prune
without -a will remove (for images) only dangling images, or images without a tag, as commented by smilebomb.
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune -a
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
As wjv comments,
There is also
docker {container,image,volume,network} prune
, which may be used to remove unused instances of just one type of object.
Introduced in commit 913e5cb, only for Docker 1.13+.
docker container prune
Thanks for sharing - for this to become visible, I'm quoting you in the answer high up in the thread
– qkrijger
Oct 6 '16 at 18:43
I upvoted you already :)
– qkrijger
Oct 7 '16 at 11:56
As usual, add the-f
flag to force the cleanup with no questions asked.
– Dirk
Feb 3 '17 at 11:21
2
should be marked as the nowdays correct answer, most of the above were right back in time, but are nodays overcomplicated
– Eugen Mayer
Jun 25 '17 at 10:59
Agreed, so much easier lately, up up you go!
– Maxim Geerinck
Jul 14 '17 at 6:44
|
show 4 more comments
With Docker 1.13 (Q4 2016), you now have:
docker system prune -a
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
docker system prune
without -a will remove (for images) only dangling images, or images without a tag, as commented by smilebomb.
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune -a
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
As wjv comments,
There is also
docker {container,image,volume,network} prune
, which may be used to remove unused instances of just one type of object.
Introduced in commit 913e5cb, only for Docker 1.13+.
docker container prune
With Docker 1.13 (Q4 2016), you now have:
docker system prune -a
will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
docker system prune
without -a will remove (for images) only dangling images, or images without a tag, as commented by smilebomb.
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune -a
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
As wjv comments,
There is also
docker {container,image,volume,network} prune
, which may be used to remove unused instances of just one type of object.
Introduced in commit 913e5cb, only for Docker 1.13+.
docker container prune
edited Jul 20 '18 at 15:04
answered Oct 4 '16 at 19:51
VonCVonC
843k29426733226
843k29426733226
Thanks for sharing - for this to become visible, I'm quoting you in the answer high up in the thread
– qkrijger
Oct 6 '16 at 18:43
I upvoted you already :)
– qkrijger
Oct 7 '16 at 11:56
As usual, add the-f
flag to force the cleanup with no questions asked.
– Dirk
Feb 3 '17 at 11:21
2
should be marked as the nowdays correct answer, most of the above were right back in time, but are nodays overcomplicated
– Eugen Mayer
Jun 25 '17 at 10:59
Agreed, so much easier lately, up up you go!
– Maxim Geerinck
Jul 14 '17 at 6:44
|
show 4 more comments
Thanks for sharing - for this to become visible, I'm quoting you in the answer high up in the thread
– qkrijger
Oct 6 '16 at 18:43
I upvoted you already :)
– qkrijger
Oct 7 '16 at 11:56
As usual, add the-f
flag to force the cleanup with no questions asked.
– Dirk
Feb 3 '17 at 11:21
2
should be marked as the nowdays correct answer, most of the above were right back in time, but are nodays overcomplicated
– Eugen Mayer
Jun 25 '17 at 10:59
Agreed, so much easier lately, up up you go!
– Maxim Geerinck
Jul 14 '17 at 6:44
Thanks for sharing - for this to become visible, I'm quoting you in the answer high up in the thread
– qkrijger
Oct 6 '16 at 18:43
Thanks for sharing - for this to become visible, I'm quoting you in the answer high up in the thread
– qkrijger
Oct 6 '16 at 18:43
I upvoted you already :)
– qkrijger
Oct 7 '16 at 11:56
I upvoted you already :)
– qkrijger
Oct 7 '16 at 11:56
As usual, add the
-f
flag to force the cleanup with no questions asked.– Dirk
Feb 3 '17 at 11:21
As usual, add the
-f
flag to force the cleanup with no questions asked.– Dirk
Feb 3 '17 at 11:21
2
2
should be marked as the nowdays correct answer, most of the above were right back in time, but are nodays overcomplicated
– Eugen Mayer
Jun 25 '17 at 10:59
should be marked as the nowdays correct answer, most of the above were right back in time, but are nodays overcomplicated
– Eugen Mayer
Jun 25 '17 at 10:59
Agreed, so much easier lately, up up you go!
– Maxim Geerinck
Jul 14 '17 at 6:44
Agreed, so much easier lately, up up you go!
– Maxim Geerinck
Jul 14 '17 at 6:44
|
show 4 more comments
It is now possible to use filtering with docker ps
:
docker rm $(docker ps -q -f status=exited)
And for images:
docker rmi $(docker images -q -f "dangling=true")
However, any of those will cause docker rm
or docker rmi
to throw an error when there are no matching containers. The older docker rm $(docker ps -aq)
trick was even worse as it tried to remove any running container, failing at each one.
Here's a cleaner script to add in your ~/.bashrc
or ~/.profile
:
# Use `docker-cleanup --dry-run` to see what would be deleted.
function docker-cleanup {
EXITED=$(docker ps -q -f status=exited)
DANGLING=$(docker images -q -f "dangling=true")
if [ "$1" == "--dry-run" ]; then
echo "==> Would stop containers:"
echo $EXITED
echo "==> And images:"
echo $DANGLING
else
if [ -n "$EXITED" ]; then
docker rm $EXITED
else
echo "No containers to remove."
fi
if [ -n "$DANGLING" ]; then
docker rmi $DANGLING
else
echo "No images to remove."
fi
fi
}
Edit: As noted below, original answer was for removing images, not containers. Updated to answer both, including new links to documentation. Thanks to Adrian (and Ryan's answer) for mentioning the new ps
filtering.
2
Note the real avantage of this - you don't accidentally kill offstorage
conatiners.
– Sobrique
Jan 10 '16 at 16:41
2
You can also usestatus=created
to get ones that are not running and have never run.
– enderland
Feb 17 '16 at 15:41
add a comment |
It is now possible to use filtering with docker ps
:
docker rm $(docker ps -q -f status=exited)
And for images:
docker rmi $(docker images -q -f "dangling=true")
However, any of those will cause docker rm
or docker rmi
to throw an error when there are no matching containers. The older docker rm $(docker ps -aq)
trick was even worse as it tried to remove any running container, failing at each one.
Here's a cleaner script to add in your ~/.bashrc
or ~/.profile
:
# Use `docker-cleanup --dry-run` to see what would be deleted.
function docker-cleanup {
EXITED=$(docker ps -q -f status=exited)
DANGLING=$(docker images -q -f "dangling=true")
if [ "$1" == "--dry-run" ]; then
echo "==> Would stop containers:"
echo $EXITED
echo "==> And images:"
echo $DANGLING
else
if [ -n "$EXITED" ]; then
docker rm $EXITED
else
echo "No containers to remove."
fi
if [ -n "$DANGLING" ]; then
docker rmi $DANGLING
else
echo "No images to remove."
fi
fi
}
Edit: As noted below, original answer was for removing images, not containers. Updated to answer both, including new links to documentation. Thanks to Adrian (and Ryan's answer) for mentioning the new ps
filtering.
2
Note the real avantage of this - you don't accidentally kill offstorage
conatiners.
– Sobrique
Jan 10 '16 at 16:41
2
You can also usestatus=created
to get ones that are not running and have never run.
– enderland
Feb 17 '16 at 15:41
add a comment |
It is now possible to use filtering with docker ps
:
docker rm $(docker ps -q -f status=exited)
And for images:
docker rmi $(docker images -q -f "dangling=true")
However, any of those will cause docker rm
or docker rmi
to throw an error when there are no matching containers. The older docker rm $(docker ps -aq)
trick was even worse as it tried to remove any running container, failing at each one.
Here's a cleaner script to add in your ~/.bashrc
or ~/.profile
:
# Use `docker-cleanup --dry-run` to see what would be deleted.
function docker-cleanup {
EXITED=$(docker ps -q -f status=exited)
DANGLING=$(docker images -q -f "dangling=true")
if [ "$1" == "--dry-run" ]; then
echo "==> Would stop containers:"
echo $EXITED
echo "==> And images:"
echo $DANGLING
else
if [ -n "$EXITED" ]; then
docker rm $EXITED
else
echo "No containers to remove."
fi
if [ -n "$DANGLING" ]; then
docker rmi $DANGLING
else
echo "No images to remove."
fi
fi
}
Edit: As noted below, original answer was for removing images, not containers. Updated to answer both, including new links to documentation. Thanks to Adrian (and Ryan's answer) for mentioning the new ps
filtering.
It is now possible to use filtering with docker ps
:
docker rm $(docker ps -q -f status=exited)
And for images:
docker rmi $(docker images -q -f "dangling=true")
However, any of those will cause docker rm
or docker rmi
to throw an error when there are no matching containers. The older docker rm $(docker ps -aq)
trick was even worse as it tried to remove any running container, failing at each one.
Here's a cleaner script to add in your ~/.bashrc
or ~/.profile
:
# Use `docker-cleanup --dry-run` to see what would be deleted.
function docker-cleanup {
EXITED=$(docker ps -q -f status=exited)
DANGLING=$(docker images -q -f "dangling=true")
if [ "$1" == "--dry-run" ]; then
echo "==> Would stop containers:"
echo $EXITED
echo "==> And images:"
echo $DANGLING
else
if [ -n "$EXITED" ]; then
docker rm $EXITED
else
echo "No containers to remove."
fi
if [ -n "$DANGLING" ]; then
docker rmi $DANGLING
else
echo "No images to remove."
fi
fi
}
Edit: As noted below, original answer was for removing images, not containers. Updated to answer both, including new links to documentation. Thanks to Adrian (and Ryan's answer) for mentioning the new ps
filtering.
edited Jul 8 '15 at 5:37
answered Jun 23 '14 at 2:07


caktuxcaktux
3,14921210
3,14921210
2
Note the real avantage of this - you don't accidentally kill offstorage
conatiners.
– Sobrique
Jan 10 '16 at 16:41
2
You can also usestatus=created
to get ones that are not running and have never run.
– enderland
Feb 17 '16 at 15:41
add a comment |
2
Note the real avantage of this - you don't accidentally kill offstorage
conatiners.
– Sobrique
Jan 10 '16 at 16:41
2
You can also usestatus=created
to get ones that are not running and have never run.
– enderland
Feb 17 '16 at 15:41
2
2
Note the real avantage of this - you don't accidentally kill off
storage
conatiners.– Sobrique
Jan 10 '16 at 16:41
Note the real avantage of this - you don't accidentally kill off
storage
conatiners.– Sobrique
Jan 10 '16 at 16:41
2
2
You can also use
status=created
to get ones that are not running and have never run.– enderland
Feb 17 '16 at 15:41
You can also use
status=created
to get ones that are not running and have never run.– enderland
Feb 17 '16 at 15:41
add a comment |
UPDATED 2017 (NEWEST)
docker container prune
This - 2017 (OLD) way
To remove ALL STOPPED CONTAINERS
docker rm $(docker ps -a -q)
To remove ALL CONTAINERS (STOPPED AND NON STOPPED)
docker rm -f $(docker ps -a -q)
worked for me on OS X
– cljk
Jan 30 '17 at 19:03
3
downvoted, since this is for sure not the way you do that in 2017 - whis would bedocker volume/container/image prune
, see stackoverflow.com/a/39860665/3625317
– Eugen Mayer
Jun 25 '17 at 11:00
4
upvoted to counter the downvote because a) this answer was from 2016, and b) plenty of folks dont run the bleeding edge.
– keen
Oct 2 '17 at 20:58
add a comment |
UPDATED 2017 (NEWEST)
docker container prune
This - 2017 (OLD) way
To remove ALL STOPPED CONTAINERS
docker rm $(docker ps -a -q)
To remove ALL CONTAINERS (STOPPED AND NON STOPPED)
docker rm -f $(docker ps -a -q)
worked for me on OS X
– cljk
Jan 30 '17 at 19:03
3
downvoted, since this is for sure not the way you do that in 2017 - whis would bedocker volume/container/image prune
, see stackoverflow.com/a/39860665/3625317
– Eugen Mayer
Jun 25 '17 at 11:00
4
upvoted to counter the downvote because a) this answer was from 2016, and b) plenty of folks dont run the bleeding edge.
– keen
Oct 2 '17 at 20:58
add a comment |
UPDATED 2017 (NEWEST)
docker container prune
This - 2017 (OLD) way
To remove ALL STOPPED CONTAINERS
docker rm $(docker ps -a -q)
To remove ALL CONTAINERS (STOPPED AND NON STOPPED)
docker rm -f $(docker ps -a -q)
UPDATED 2017 (NEWEST)
docker container prune
This - 2017 (OLD) way
To remove ALL STOPPED CONTAINERS
docker rm $(docker ps -a -q)
To remove ALL CONTAINERS (STOPPED AND NON STOPPED)
docker rm -f $(docker ps -a -q)
edited Sep 11 '17 at 19:46
answered Aug 8 '16 at 20:37


levilevi
14.3k24553
14.3k24553
worked for me on OS X
– cljk
Jan 30 '17 at 19:03
3
downvoted, since this is for sure not the way you do that in 2017 - whis would bedocker volume/container/image prune
, see stackoverflow.com/a/39860665/3625317
– Eugen Mayer
Jun 25 '17 at 11:00
4
upvoted to counter the downvote because a) this answer was from 2016, and b) plenty of folks dont run the bleeding edge.
– keen
Oct 2 '17 at 20:58
add a comment |
worked for me on OS X
– cljk
Jan 30 '17 at 19:03
3
downvoted, since this is for sure not the way you do that in 2017 - whis would bedocker volume/container/image prune
, see stackoverflow.com/a/39860665/3625317
– Eugen Mayer
Jun 25 '17 at 11:00
4
upvoted to counter the downvote because a) this answer was from 2016, and b) plenty of folks dont run the bleeding edge.
– keen
Oct 2 '17 at 20:58
worked for me on OS X
– cljk
Jan 30 '17 at 19:03
worked for me on OS X
– cljk
Jan 30 '17 at 19:03
3
3
downvoted, since this is for sure not the way you do that in 2017 - whis would be
docker volume/container/image prune
, see stackoverflow.com/a/39860665/3625317– Eugen Mayer
Jun 25 '17 at 11:00
downvoted, since this is for sure not the way you do that in 2017 - whis would be
docker volume/container/image prune
, see stackoverflow.com/a/39860665/3625317– Eugen Mayer
Jun 25 '17 at 11:00
4
4
upvoted to counter the downvote because a) this answer was from 2016, and b) plenty of folks dont run the bleeding edge.
– keen
Oct 2 '17 at 20:58
upvoted to counter the downvote because a) this answer was from 2016, and b) plenty of folks dont run the bleeding edge.
– keen
Oct 2 '17 at 20:58
add a comment |
Remove all stopped containers:
docker rm $(docker ps -a | grep Exited | awk '{print $1}')
From the comment by pauk960:
Since version 1.3.0 you can use filters with
docker ps
, instead ofgrep Exited
usedocker ps -a -f status=exited
. And if you use-q
with it you can get container IDs only instead of full output, no need to use awk for that.
add a comment |
Remove all stopped containers:
docker rm $(docker ps -a | grep Exited | awk '{print $1}')
From the comment by pauk960:
Since version 1.3.0 you can use filters with
docker ps
, instead ofgrep Exited
usedocker ps -a -f status=exited
. And if you use-q
with it you can get container IDs only instead of full output, no need to use awk for that.
add a comment |
Remove all stopped containers:
docker rm $(docker ps -a | grep Exited | awk '{print $1}')
From the comment by pauk960:
Since version 1.3.0 you can use filters with
docker ps
, instead ofgrep Exited
usedocker ps -a -f status=exited
. And if you use-q
with it you can get container IDs only instead of full output, no need to use awk for that.
Remove all stopped containers:
docker rm $(docker ps -a | grep Exited | awk '{print $1}')
From the comment by pauk960:
Since version 1.3.0 you can use filters with
docker ps
, instead ofgrep Exited
usedocker ps -a -f status=exited
. And if you use-q
with it you can get container IDs only instead of full output, no need to use awk for that.
edited May 23 '17 at 12:03
Community♦
11
11
answered Jul 10 '14 at 16:36
montellsmontells
3,22933544
3,22933544
add a comment |
add a comment |
If you do not like to remove all containers, you can select all containers created before or after a specific container with docker ps --before <container-ID>
or with docker ps --since <container-ID>
. This feature is at least in Docker version 0.6.5.
Let's say you have developed your system, and now it is working, but there are a number of containers left. You want to remove containers created before that working version. Determine the ID of the working container with docker ps
.
Remove containers created before an other container
docker rm $(docker ps --before 9c49c11c8d21 -q)
Another example. You have your database already running on a Docker container. You have developed your application to run on another container and now you have a number of unneeded containers.
Remove containers created after a certain container
docker rm $(docker ps --since a6ca4661ec7f -q)
Docker stores containers in /var/lib/docker/containers
in Ubuntu. I think extra containers do no other harm, but take up disk space.
add a comment |
If you do not like to remove all containers, you can select all containers created before or after a specific container with docker ps --before <container-ID>
or with docker ps --since <container-ID>
. This feature is at least in Docker version 0.6.5.
Let's say you have developed your system, and now it is working, but there are a number of containers left. You want to remove containers created before that working version. Determine the ID of the working container with docker ps
.
Remove containers created before an other container
docker rm $(docker ps --before 9c49c11c8d21 -q)
Another example. You have your database already running on a Docker container. You have developed your application to run on another container and now you have a number of unneeded containers.
Remove containers created after a certain container
docker rm $(docker ps --since a6ca4661ec7f -q)
Docker stores containers in /var/lib/docker/containers
in Ubuntu. I think extra containers do no other harm, but take up disk space.
add a comment |
If you do not like to remove all containers, you can select all containers created before or after a specific container with docker ps --before <container-ID>
or with docker ps --since <container-ID>
. This feature is at least in Docker version 0.6.5.
Let's say you have developed your system, and now it is working, but there are a number of containers left. You want to remove containers created before that working version. Determine the ID of the working container with docker ps
.
Remove containers created before an other container
docker rm $(docker ps --before 9c49c11c8d21 -q)
Another example. You have your database already running on a Docker container. You have developed your application to run on another container and now you have a number of unneeded containers.
Remove containers created after a certain container
docker rm $(docker ps --since a6ca4661ec7f -q)
Docker stores containers in /var/lib/docker/containers
in Ubuntu. I think extra containers do no other harm, but take up disk space.
If you do not like to remove all containers, you can select all containers created before or after a specific container with docker ps --before <container-ID>
or with docker ps --since <container-ID>
. This feature is at least in Docker version 0.6.5.
Let's say you have developed your system, and now it is working, but there are a number of containers left. You want to remove containers created before that working version. Determine the ID of the working container with docker ps
.
Remove containers created before an other container
docker rm $(docker ps --before 9c49c11c8d21 -q)
Another example. You have your database already running on a Docker container. You have developed your application to run on another container and now you have a number of unneeded containers.
Remove containers created after a certain container
docker rm $(docker ps --since a6ca4661ec7f -q)
Docker stores containers in /var/lib/docker/containers
in Ubuntu. I think extra containers do no other harm, but take up disk space.
edited Jul 23 '18 at 17:19


Peter Mortensen
13.7k1986112
13.7k1986112
answered Nov 7 '13 at 7:35


vesakovesako
1,494196
1,494196
add a comment |
add a comment |
Update: As of Docker version 1.13 (released January 2017), you can issue the following command to clean up stopped containers, unused volumes, dangling images and unused networks:
docker system prune
If you want to insure that you're only deleting containers which have an exited
status, use this:
docker ps -aq -f status=exited | xargs docker rm
Similarly, if you're cleaning up docker stuff, you can get rid of untagged, unnamed images in this way:
docker images -q --no-trunc -f dangling=true | xargs docker rmi
add a comment |
Update: As of Docker version 1.13 (released January 2017), you can issue the following command to clean up stopped containers, unused volumes, dangling images and unused networks:
docker system prune
If you want to insure that you're only deleting containers which have an exited
status, use this:
docker ps -aq -f status=exited | xargs docker rm
Similarly, if you're cleaning up docker stuff, you can get rid of untagged, unnamed images in this way:
docker images -q --no-trunc -f dangling=true | xargs docker rmi
add a comment |
Update: As of Docker version 1.13 (released January 2017), you can issue the following command to clean up stopped containers, unused volumes, dangling images and unused networks:
docker system prune
If you want to insure that you're only deleting containers which have an exited
status, use this:
docker ps -aq -f status=exited | xargs docker rm
Similarly, if you're cleaning up docker stuff, you can get rid of untagged, unnamed images in this way:
docker images -q --no-trunc -f dangling=true | xargs docker rmi
Update: As of Docker version 1.13 (released January 2017), you can issue the following command to clean up stopped containers, unused volumes, dangling images and unused networks:
docker system prune
If you want to insure that you're only deleting containers which have an exited
status, use this:
docker ps -aq -f status=exited | xargs docker rm
Similarly, if you're cleaning up docker stuff, you can get rid of untagged, unnamed images in this way:
docker images -q --no-trunc -f dangling=true | xargs docker rmi
edited Jan 22 '17 at 10:35
answered Apr 1 '16 at 23:54


L0j1kL0j1k
9,10863759
9,10863759
add a comment |
add a comment |
Here is my docker-cleanup
script, which removes untagged containers and images. Please check the source for any updates.
#!/bin/sh
# Cleanup docker files: untagged containers and images.
#
# Use `docker-cleanup -n` for a dry run to see what would be deleted.
untagged_containers() {
# Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
docker ps -a | awk '$2 ~ "[0-9a-f]{12}" {print $'$1'}'
}
untagged_images() {
# Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
# NOTE: intermediate images (via -a) seem to only cause
# "Error: Conflict, foobarid wasn't deleted" messages.
# Might be useful sometimes when Docker messed things up?!
# docker images -a | awk '$1 == "<none>" {print $'$1'}'
docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
}
# Dry-run.
if [ "$1" = "-n" ]; then
echo "=== Containers with uncommitted images: ==="
untagged_containers 0
echo
echo "=== Uncommitted images: ==="
untagged_images 0
exit
fi
# Remove containers with untagged images.
echo "Removing containers:" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
# Remove untagged images
echo "Removing images:" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi
Source: https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup
@BradMurray Thanks! You might also like these: github.com/blueyed/dotfiles/commit/…
– blueyed
Jun 7 '14 at 23:06
3
Does not work on OSX because of thexargs --no-run-if-empty
option
– caktux
Jul 14 '15 at 5:36
add a comment |
Here is my docker-cleanup
script, which removes untagged containers and images. Please check the source for any updates.
#!/bin/sh
# Cleanup docker files: untagged containers and images.
#
# Use `docker-cleanup -n` for a dry run to see what would be deleted.
untagged_containers() {
# Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
docker ps -a | awk '$2 ~ "[0-9a-f]{12}" {print $'$1'}'
}
untagged_images() {
# Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
# NOTE: intermediate images (via -a) seem to only cause
# "Error: Conflict, foobarid wasn't deleted" messages.
# Might be useful sometimes when Docker messed things up?!
# docker images -a | awk '$1 == "<none>" {print $'$1'}'
docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
}
# Dry-run.
if [ "$1" = "-n" ]; then
echo "=== Containers with uncommitted images: ==="
untagged_containers 0
echo
echo "=== Uncommitted images: ==="
untagged_images 0
exit
fi
# Remove containers with untagged images.
echo "Removing containers:" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
# Remove untagged images
echo "Removing images:" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi
Source: https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup
@BradMurray Thanks! You might also like these: github.com/blueyed/dotfiles/commit/…
– blueyed
Jun 7 '14 at 23:06
3
Does not work on OSX because of thexargs --no-run-if-empty
option
– caktux
Jul 14 '15 at 5:36
add a comment |
Here is my docker-cleanup
script, which removes untagged containers and images. Please check the source for any updates.
#!/bin/sh
# Cleanup docker files: untagged containers and images.
#
# Use `docker-cleanup -n` for a dry run to see what would be deleted.
untagged_containers() {
# Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
docker ps -a | awk '$2 ~ "[0-9a-f]{12}" {print $'$1'}'
}
untagged_images() {
# Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
# NOTE: intermediate images (via -a) seem to only cause
# "Error: Conflict, foobarid wasn't deleted" messages.
# Might be useful sometimes when Docker messed things up?!
# docker images -a | awk '$1 == "<none>" {print $'$1'}'
docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
}
# Dry-run.
if [ "$1" = "-n" ]; then
echo "=== Containers with uncommitted images: ==="
untagged_containers 0
echo
echo "=== Uncommitted images: ==="
untagged_images 0
exit
fi
# Remove containers with untagged images.
echo "Removing containers:" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
# Remove untagged images
echo "Removing images:" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi
Source: https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup
Here is my docker-cleanup
script, which removes untagged containers and images. Please check the source for any updates.
#!/bin/sh
# Cleanup docker files: untagged containers and images.
#
# Use `docker-cleanup -n` for a dry run to see what would be deleted.
untagged_containers() {
# Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
docker ps -a | awk '$2 ~ "[0-9a-f]{12}" {print $'$1'}'
}
untagged_images() {
# Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
# NOTE: intermediate images (via -a) seem to only cause
# "Error: Conflict, foobarid wasn't deleted" messages.
# Might be useful sometimes when Docker messed things up?!
# docker images -a | awk '$1 == "<none>" {print $'$1'}'
docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
}
# Dry-run.
if [ "$1" = "-n" ]; then
echo "=== Containers with uncommitted images: ==="
untagged_containers 0
echo
echo "=== Uncommitted images: ==="
untagged_images 0
exit
fi
# Remove containers with untagged images.
echo "Removing containers:" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
# Remove untagged images
echo "Removing images:" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi
Source: https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup
edited Jul 23 '18 at 17:21


Peter Mortensen
13.7k1986112
13.7k1986112
answered May 8 '14 at 11:15
blueyedblueyed
20.4k35860
20.4k35860
@BradMurray Thanks! You might also like these: github.com/blueyed/dotfiles/commit/…
– blueyed
Jun 7 '14 at 23:06
3
Does not work on OSX because of thexargs --no-run-if-empty
option
– caktux
Jul 14 '15 at 5:36
add a comment |
@BradMurray Thanks! You might also like these: github.com/blueyed/dotfiles/commit/…
– blueyed
Jun 7 '14 at 23:06
3
Does not work on OSX because of thexargs --no-run-if-empty
option
– caktux
Jul 14 '15 at 5:36
@BradMurray Thanks! You might also like these: github.com/blueyed/dotfiles/commit/…
– blueyed
Jun 7 '14 at 23:06
@BradMurray Thanks! You might also like these: github.com/blueyed/dotfiles/commit/…
– blueyed
Jun 7 '14 at 23:06
3
3
Does not work on OSX because of the
xargs --no-run-if-empty
option– caktux
Jul 14 '15 at 5:36
Does not work on OSX because of the
xargs --no-run-if-empty
option– caktux
Jul 14 '15 at 5:36
add a comment |
First, stop running containers before attempting to remove them
Remove running containers
docker rm $(docker stop -t=1 $(docker ps -q))
You could use kill
instead of stop
. In my case I prefer stop
since I tend to rerun them vs. creating a new one every time so I try to shut them down nicely.
Note: Trying to stop a container will give you an error:
Error: Impossible to remove a running container, please stop it first
Remove all containers
docker rm $(docker ps -a -q)
add a comment |
First, stop running containers before attempting to remove them
Remove running containers
docker rm $(docker stop -t=1 $(docker ps -q))
You could use kill
instead of stop
. In my case I prefer stop
since I tend to rerun them vs. creating a new one every time so I try to shut them down nicely.
Note: Trying to stop a container will give you an error:
Error: Impossible to remove a running container, please stop it first
Remove all containers
docker rm $(docker ps -a -q)
add a comment |
First, stop running containers before attempting to remove them
Remove running containers
docker rm $(docker stop -t=1 $(docker ps -q))
You could use kill
instead of stop
. In my case I prefer stop
since I tend to rerun them vs. creating a new one every time so I try to shut them down nicely.
Note: Trying to stop a container will give you an error:
Error: Impossible to remove a running container, please stop it first
Remove all containers
docker rm $(docker ps -a -q)
First, stop running containers before attempting to remove them
Remove running containers
docker rm $(docker stop -t=1 $(docker ps -q))
You could use kill
instead of stop
. In my case I prefer stop
since I tend to rerun them vs. creating a new one every time so I try to shut them down nicely.
Note: Trying to stop a container will give you an error:
Error: Impossible to remove a running container, please stop it first
Remove all containers
docker rm $(docker ps -a -q)
edited Jul 23 '18 at 17:18


Peter Mortensen
13.7k1986112
13.7k1986112
answered Oct 31 '13 at 16:22
UlisesUlises
11.8k42743
11.8k42743
add a comment |
add a comment |
Removing all containers from Windows shell:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
I'll include this in my answer since that is first page. Thanks
– qkrijger
Apr 17 '16 at 10:58
"from Windows shell"? Do you mean "using the Windows shell (CMD)"?
– Peter Mortensen
Jul 23 '18 at 17:39
add a comment |
Removing all containers from Windows shell:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
I'll include this in my answer since that is first page. Thanks
– qkrijger
Apr 17 '16 at 10:58
"from Windows shell"? Do you mean "using the Windows shell (CMD)"?
– Peter Mortensen
Jul 23 '18 at 17:39
add a comment |
Removing all containers from Windows shell:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
Removing all containers from Windows shell:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
answered Apr 13 '16 at 11:45
Bartosz BilickiBartosz Bilicki
6,55054271
6,55054271
I'll include this in my answer since that is first page. Thanks
– qkrijger
Apr 17 '16 at 10:58
"from Windows shell"? Do you mean "using the Windows shell (CMD)"?
– Peter Mortensen
Jul 23 '18 at 17:39
add a comment |
I'll include this in my answer since that is first page. Thanks
– qkrijger
Apr 17 '16 at 10:58
"from Windows shell"? Do you mean "using the Windows shell (CMD)"?
– Peter Mortensen
Jul 23 '18 at 17:39
I'll include this in my answer since that is first page. Thanks
– qkrijger
Apr 17 '16 at 10:58
I'll include this in my answer since that is first page. Thanks
– qkrijger
Apr 17 '16 at 10:58
"from Windows shell"? Do you mean "using the Windows shell (CMD)"?
– Peter Mortensen
Jul 23 '18 at 17:39
"from Windows shell"? Do you mean "using the Windows shell (CMD)"?
– Peter Mortensen
Jul 23 '18 at 17:39
add a comment |
https://github.com/HardySimpson/docker-cleanup
Docker cleanup
A tiny all-in-one shell, which removes:
- Containers that not running more than one day ago
- Images that don't belong to any remaining container
Intend to run as a crontab job
Feature
- It will remove all
<none>:<none>
images - If the image has multiple repo:tag references to it, it will remove all repo:tag except with running a container. Actually it is a nature of "docker rmi".
- Many error message will be show on screen, and you can decide to
2>/dev/null
or not - Learn something from docker-gc, and fix its problem (it can not remove image that has mutliple repo:tag)
add a comment |
https://github.com/HardySimpson/docker-cleanup
Docker cleanup
A tiny all-in-one shell, which removes:
- Containers that not running more than one day ago
- Images that don't belong to any remaining container
Intend to run as a crontab job
Feature
- It will remove all
<none>:<none>
images - If the image has multiple repo:tag references to it, it will remove all repo:tag except with running a container. Actually it is a nature of "docker rmi".
- Many error message will be show on screen, and you can decide to
2>/dev/null
or not - Learn something from docker-gc, and fix its problem (it can not remove image that has mutliple repo:tag)
add a comment |
https://github.com/HardySimpson/docker-cleanup
Docker cleanup
A tiny all-in-one shell, which removes:
- Containers that not running more than one day ago
- Images that don't belong to any remaining container
Intend to run as a crontab job
Feature
- It will remove all
<none>:<none>
images - If the image has multiple repo:tag references to it, it will remove all repo:tag except with running a container. Actually it is a nature of "docker rmi".
- Many error message will be show on screen, and you can decide to
2>/dev/null
or not - Learn something from docker-gc, and fix its problem (it can not remove image that has mutliple repo:tag)
https://github.com/HardySimpson/docker-cleanup
Docker cleanup
A tiny all-in-one shell, which removes:
- Containers that not running more than one day ago
- Images that don't belong to any remaining container
Intend to run as a crontab job
Feature
- It will remove all
<none>:<none>
images - If the image has multiple repo:tag references to it, it will remove all repo:tag except with running a container. Actually it is a nature of "docker rmi".
- Many error message will be show on screen, and you can decide to
2>/dev/null
or not - Learn something from docker-gc, and fix its problem (it can not remove image that has mutliple repo:tag)
edited Mar 3 '16 at 19:50


Peter Mortensen
13.7k1986112
13.7k1986112
answered May 15 '15 at 7:41
HardySimpsonHardySimpson
5921711
5921711
add a comment |
add a comment |
Use:
docker rm -f $(docker ps -a -q)
It forcefully stops and removes all containers present locally.
Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge.
– Brian Tompsett - 汤莱恩
Sep 7 '16 at 14:25
In ubuntu 16.04, docker requires sudo, so you have to writesudo docker rm -f $(sudo docker ps -a -q)
.
– Elliott
Sep 21 '16 at 15:47
as a plus, this also works in Windows under powershell!
– Robert Ivanc
Oct 21 '16 at 14:34
@Elliott I thinksudo
is required everywhere unless you add your user to the 'docker' group. I use ubuntu 16.10 without sudo by adding my user to the docker group.
– emory
Mar 26 '17 at 11:38
add a comment |
Use:
docker rm -f $(docker ps -a -q)
It forcefully stops and removes all containers present locally.
Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge.
– Brian Tompsett - 汤莱恩
Sep 7 '16 at 14:25
In ubuntu 16.04, docker requires sudo, so you have to writesudo docker rm -f $(sudo docker ps -a -q)
.
– Elliott
Sep 21 '16 at 15:47
as a plus, this also works in Windows under powershell!
– Robert Ivanc
Oct 21 '16 at 14:34
@Elliott I thinksudo
is required everywhere unless you add your user to the 'docker' group. I use ubuntu 16.10 without sudo by adding my user to the docker group.
– emory
Mar 26 '17 at 11:38
add a comment |
Use:
docker rm -f $(docker ps -a -q)
It forcefully stops and removes all containers present locally.
Use:
docker rm -f $(docker ps -a -q)
It forcefully stops and removes all containers present locally.
edited Mar 10 '17 at 3:55


Peter Mortensen
13.7k1986112
13.7k1986112
answered Sep 7 '16 at 10:44


Shashank GShashank G
391616
391616
Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge.
– Brian Tompsett - 汤莱恩
Sep 7 '16 at 14:25
In ubuntu 16.04, docker requires sudo, so you have to writesudo docker rm -f $(sudo docker ps -a -q)
.
– Elliott
Sep 21 '16 at 15:47
as a plus, this also works in Windows under powershell!
– Robert Ivanc
Oct 21 '16 at 14:34
@Elliott I thinksudo
is required everywhere unless you add your user to the 'docker' group. I use ubuntu 16.10 without sudo by adding my user to the docker group.
– emory
Mar 26 '17 at 11:38
add a comment |
Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge.
– Brian Tompsett - 汤莱恩
Sep 7 '16 at 14:25
In ubuntu 16.04, docker requires sudo, so you have to writesudo docker rm -f $(sudo docker ps -a -q)
.
– Elliott
Sep 21 '16 at 15:47
as a plus, this also works in Windows under powershell!
– Robert Ivanc
Oct 21 '16 at 14:34
@Elliott I thinksudo
is required everywhere unless you add your user to the 'docker' group. I use ubuntu 16.10 without sudo by adding my user to the docker group.
– emory
Mar 26 '17 at 11:38
Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge.
– Brian Tompsett - 汤莱恩
Sep 7 '16 at 14:25
Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge.
– Brian Tompsett - 汤莱恩
Sep 7 '16 at 14:25
In ubuntu 16.04, docker requires sudo, so you have to write
sudo docker rm -f $(sudo docker ps -a -q)
.– Elliott
Sep 21 '16 at 15:47
In ubuntu 16.04, docker requires sudo, so you have to write
sudo docker rm -f $(sudo docker ps -a -q)
.– Elliott
Sep 21 '16 at 15:47
as a plus, this also works in Windows under powershell!
– Robert Ivanc
Oct 21 '16 at 14:34
as a plus, this also works in Windows under powershell!
– Robert Ivanc
Oct 21 '16 at 14:34
@Elliott I think
sudo
is required everywhere unless you add your user to the 'docker' group. I use ubuntu 16.10 without sudo by adding my user to the docker group.– emory
Mar 26 '17 at 11:38
@Elliott I think
sudo
is required everywhere unless you add your user to the 'docker' group. I use ubuntu 16.10 without sudo by adding my user to the docker group.– emory
Mar 26 '17 at 11:38
add a comment |
Remove all stopped containers.
sudo docker rm $(sudo docker ps -a -q)
This will remove all stopped containers by getting a list of all containers with docker ps -a -q and passing their ids to docker rm. This should not remove any running containers, and it will tell you it can’t remove a running image.
Remove all untagged images
Now you want to clean up old images to save some space.
sudo docker rmi $(sudo docker images -q --filter "dangling=true")
Don't forget to delete docker volumes by specifying the -v option (sudo docker rm -v $(sudo docker ps -a -q))
– vcarel
Nov 9 '15 at 10:30
Didn't look this up, but I guess the same as:docker ps -aq | xargs docker rm -v
– quine
Mar 23 '18 at 20:24
This only works on Linux, not Windows... Butdocker rm -f $(docker ps -a -q)
works in PowerShell / Docker for Windows.
– Peter Mortensen
Jul 24 '18 at 7:58
add a comment |
Remove all stopped containers.
sudo docker rm $(sudo docker ps -a -q)
This will remove all stopped containers by getting a list of all containers with docker ps -a -q and passing their ids to docker rm. This should not remove any running containers, and it will tell you it can’t remove a running image.
Remove all untagged images
Now you want to clean up old images to save some space.
sudo docker rmi $(sudo docker images -q --filter "dangling=true")
Don't forget to delete docker volumes by specifying the -v option (sudo docker rm -v $(sudo docker ps -a -q))
– vcarel
Nov 9 '15 at 10:30
Didn't look this up, but I guess the same as:docker ps -aq | xargs docker rm -v
– quine
Mar 23 '18 at 20:24
This only works on Linux, not Windows... Butdocker rm -f $(docker ps -a -q)
works in PowerShell / Docker for Windows.
– Peter Mortensen
Jul 24 '18 at 7:58
add a comment |
Remove all stopped containers.
sudo docker rm $(sudo docker ps -a -q)
This will remove all stopped containers by getting a list of all containers with docker ps -a -q and passing their ids to docker rm. This should not remove any running containers, and it will tell you it can’t remove a running image.
Remove all untagged images
Now you want to clean up old images to save some space.
sudo docker rmi $(sudo docker images -q --filter "dangling=true")
Remove all stopped containers.
sudo docker rm $(sudo docker ps -a -q)
This will remove all stopped containers by getting a list of all containers with docker ps -a -q and passing their ids to docker rm. This should not remove any running containers, and it will tell you it can’t remove a running image.
Remove all untagged images
Now you want to clean up old images to save some space.
sudo docker rmi $(sudo docker images -q --filter "dangling=true")
answered Nov 5 '15 at 12:25
Ric_HarveyRic_Harvey
8616
8616
Don't forget to delete docker volumes by specifying the -v option (sudo docker rm -v $(sudo docker ps -a -q))
– vcarel
Nov 9 '15 at 10:30
Didn't look this up, but I guess the same as:docker ps -aq | xargs docker rm -v
– quine
Mar 23 '18 at 20:24
This only works on Linux, not Windows... Butdocker rm -f $(docker ps -a -q)
works in PowerShell / Docker for Windows.
– Peter Mortensen
Jul 24 '18 at 7:58
add a comment |
Don't forget to delete docker volumes by specifying the -v option (sudo docker rm -v $(sudo docker ps -a -q))
– vcarel
Nov 9 '15 at 10:30
Didn't look this up, but I guess the same as:docker ps -aq | xargs docker rm -v
– quine
Mar 23 '18 at 20:24
This only works on Linux, not Windows... Butdocker rm -f $(docker ps -a -q)
works in PowerShell / Docker for Windows.
– Peter Mortensen
Jul 24 '18 at 7:58
Don't forget to delete docker volumes by specifying the -v option (sudo docker rm -v $(sudo docker ps -a -q))
– vcarel
Nov 9 '15 at 10:30
Don't forget to delete docker volumes by specifying the -v option (sudo docker rm -v $(sudo docker ps -a -q))
– vcarel
Nov 9 '15 at 10:30
Didn't look this up, but I guess the same as:
docker ps -aq | xargs docker rm -v
– quine
Mar 23 '18 at 20:24
Didn't look this up, but I guess the same as:
docker ps -aq | xargs docker rm -v
– quine
Mar 23 '18 at 20:24
This only works on Linux, not Windows... But
docker rm -f $(docker ps -a -q)
works in PowerShell / Docker for Windows.– Peter Mortensen
Jul 24 '18 at 7:58
This only works on Linux, not Windows... But
docker rm -f $(docker ps -a -q)
works in PowerShell / Docker for Windows.– Peter Mortensen
Jul 24 '18 at 7:58
add a comment |
So, personally I recommend doing this as part of your deploy script for both images and containers, keeping only the most recent n containers and images. I tag my Docker images with the same versioning schema I use with git tag
as well as always tagging the latest Docker image with "latest." This means that without cleaning up anything, my Docker images wind up looking like:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
some_repo/some_image 0.0.5 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image latest 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image 0.0.4 0beabfa514ea 45 hours ago 925.4 MB
some_repo/some_image 0.0.3 54302cd10bf2 6 days ago 978.5 MB
some_repo/some_image 0.0.2 0078b30f3d9a 7 days ago 978.5 MB
some_repo/some_image 0.0.1 sdfgdf0f3d9a 8 days ago 938.5 MB
Now, of course I don't want to keep all my images (or containers) going back to perpetuity on all my production boxes. I just want the last 3 or 4 for rollbacks and to get rid of everything else. Unix's tail
is your best friend here. Since docker images
and docker ps
both order by date, we can just use tail
to select all but the top three and remove them:
docker rmi $(docker images -q | tail -n +4)
Run that along with your deploy scripts (or locally) to always keep just enough images to comfortably roll back without taking up too much room or cluttering stuff up with old images.
Personally, I only keep one container on my production box at any time, but you can do the same sort of thing with containers if you want more:
docker rm $(docker ps -aq | tail -n +4)
Finally, in my simplified example we're only dealing with one repository at a time, but if you had more, you can just get a bit more sophisticated with the same idea. Say I just want to keep the last three images from some_repo/some_image. I can just mix in grep
and awk
and be on my way:
docker rmi $(docker images -a | grep 'some_repo/some_image' | awk '{print $3}' | tail -n +4)
Again, the same idea applies to containers, but you get it by this point so I'll stop giving examples.
add a comment |
So, personally I recommend doing this as part of your deploy script for both images and containers, keeping only the most recent n containers and images. I tag my Docker images with the same versioning schema I use with git tag
as well as always tagging the latest Docker image with "latest." This means that without cleaning up anything, my Docker images wind up looking like:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
some_repo/some_image 0.0.5 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image latest 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image 0.0.4 0beabfa514ea 45 hours ago 925.4 MB
some_repo/some_image 0.0.3 54302cd10bf2 6 days ago 978.5 MB
some_repo/some_image 0.0.2 0078b30f3d9a 7 days ago 978.5 MB
some_repo/some_image 0.0.1 sdfgdf0f3d9a 8 days ago 938.5 MB
Now, of course I don't want to keep all my images (or containers) going back to perpetuity on all my production boxes. I just want the last 3 or 4 for rollbacks and to get rid of everything else. Unix's tail
is your best friend here. Since docker images
and docker ps
both order by date, we can just use tail
to select all but the top three and remove them:
docker rmi $(docker images -q | tail -n +4)
Run that along with your deploy scripts (or locally) to always keep just enough images to comfortably roll back without taking up too much room or cluttering stuff up with old images.
Personally, I only keep one container on my production box at any time, but you can do the same sort of thing with containers if you want more:
docker rm $(docker ps -aq | tail -n +4)
Finally, in my simplified example we're only dealing with one repository at a time, but if you had more, you can just get a bit more sophisticated with the same idea. Say I just want to keep the last three images from some_repo/some_image. I can just mix in grep
and awk
and be on my way:
docker rmi $(docker images -a | grep 'some_repo/some_image' | awk '{print $3}' | tail -n +4)
Again, the same idea applies to containers, but you get it by this point so I'll stop giving examples.
add a comment |
So, personally I recommend doing this as part of your deploy script for both images and containers, keeping only the most recent n containers and images. I tag my Docker images with the same versioning schema I use with git tag
as well as always tagging the latest Docker image with "latest." This means that without cleaning up anything, my Docker images wind up looking like:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
some_repo/some_image 0.0.5 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image latest 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image 0.0.4 0beabfa514ea 45 hours ago 925.4 MB
some_repo/some_image 0.0.3 54302cd10bf2 6 days ago 978.5 MB
some_repo/some_image 0.0.2 0078b30f3d9a 7 days ago 978.5 MB
some_repo/some_image 0.0.1 sdfgdf0f3d9a 8 days ago 938.5 MB
Now, of course I don't want to keep all my images (or containers) going back to perpetuity on all my production boxes. I just want the last 3 or 4 for rollbacks and to get rid of everything else. Unix's tail
is your best friend here. Since docker images
and docker ps
both order by date, we can just use tail
to select all but the top three and remove them:
docker rmi $(docker images -q | tail -n +4)
Run that along with your deploy scripts (or locally) to always keep just enough images to comfortably roll back without taking up too much room or cluttering stuff up with old images.
Personally, I only keep one container on my production box at any time, but you can do the same sort of thing with containers if you want more:
docker rm $(docker ps -aq | tail -n +4)
Finally, in my simplified example we're only dealing with one repository at a time, but if you had more, you can just get a bit more sophisticated with the same idea. Say I just want to keep the last three images from some_repo/some_image. I can just mix in grep
and awk
and be on my way:
docker rmi $(docker images -a | grep 'some_repo/some_image' | awk '{print $3}' | tail -n +4)
Again, the same idea applies to containers, but you get it by this point so I'll stop giving examples.
So, personally I recommend doing this as part of your deploy script for both images and containers, keeping only the most recent n containers and images. I tag my Docker images with the same versioning schema I use with git tag
as well as always tagging the latest Docker image with "latest." This means that without cleaning up anything, my Docker images wind up looking like:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
some_repo/some_image 0.0.5 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image latest 8f1a7c7ba93c 23 hours ago 925.4 MB
some_repo/some_image 0.0.4 0beabfa514ea 45 hours ago 925.4 MB
some_repo/some_image 0.0.3 54302cd10bf2 6 days ago 978.5 MB
some_repo/some_image 0.0.2 0078b30f3d9a 7 days ago 978.5 MB
some_repo/some_image 0.0.1 sdfgdf0f3d9a 8 days ago 938.5 MB
Now, of course I don't want to keep all my images (or containers) going back to perpetuity on all my production boxes. I just want the last 3 or 4 for rollbacks and to get rid of everything else. Unix's tail
is your best friend here. Since docker images
and docker ps
both order by date, we can just use tail
to select all but the top three and remove them:
docker rmi $(docker images -q | tail -n +4)
Run that along with your deploy scripts (or locally) to always keep just enough images to comfortably roll back without taking up too much room or cluttering stuff up with old images.
Personally, I only keep one container on my production box at any time, but you can do the same sort of thing with containers if you want more:
docker rm $(docker ps -aq | tail -n +4)
Finally, in my simplified example we're only dealing with one repository at a time, but if you had more, you can just get a bit more sophisticated with the same idea. Say I just want to keep the last three images from some_repo/some_image. I can just mix in grep
and awk
and be on my way:
docker rmi $(docker images -a | grep 'some_repo/some_image' | awk '{print $3}' | tail -n +4)
Again, the same idea applies to containers, but you get it by this point so I'll stop giving examples.
edited Jul 23 '18 at 17:25


Peter Mortensen
13.7k1986112
13.7k1986112
answered Dec 25 '14 at 23:20
EliEli
16.5k22103168
16.5k22103168
add a comment |
add a comment |
Remove 5 oldest containers:
docker rm `docker ps -aq | tail -n 5`
See how many containers there are left:
docker ps -aq | wc -l
add a comment |
Remove 5 oldest containers:
docker rm `docker ps -aq | tail -n 5`
See how many containers there are left:
docker ps -aq | wc -l
add a comment |
Remove 5 oldest containers:
docker rm `docker ps -aq | tail -n 5`
See how many containers there are left:
docker ps -aq | wc -l
Remove 5 oldest containers:
docker rm `docker ps -aq | tail -n 5`
See how many containers there are left:
docker ps -aq | wc -l
answered Aug 8 '14 at 9:32
PithikosPithikos
7,423117095
7,423117095
add a comment |
add a comment |
New way:
spotify/docker-gc play the trick.
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc spotify/docker-gc
- Containers that exited more than an hour ago are removed.
- Images that don't belong to any remaining container after that are removed
It has supported environmental settings
Forcing deletion of images that have multiple tags
FORCE_IMAGE_REMOVAL=1
Forcing deletion of containers
FORCE_CONTAINER_REMOVAL=1
Excluding Recently Exited Containers and Images From Garbage Collection
GRACE_PERIOD_SECONDS=86400
This setting also prevents the removal of images that have been created less than GRACE_PERIOD_SECONDS seconds ago.
Dry run
DRY_RUN=1
Cleaning up orphaned container volumes
CLEAN_UP_VOLUMES=1
Reference: docker-gc
Old way to do:
delete old, non-running containers
docker ps -a -q -f status=exited | xargs --no-run-if-empty docker rm
OR
docker rm $(docker ps -a -q)
delete all images associated with non-running docker containers
docker images -q | xargs --no-run-if-empty docker rmi
cleanup orphaned docker volumes for docker version 1.10.x and above
docker volume ls -qf dangling=true | xargs -r docker volume rm
Based on time period
docker ps -a | grep "weeks ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "days ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "hours ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
1
Interesting alternative. +1
– VonC
Nov 20 '16 at 12:26
add a comment |
New way:
spotify/docker-gc play the trick.
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc spotify/docker-gc
- Containers that exited more than an hour ago are removed.
- Images that don't belong to any remaining container after that are removed
It has supported environmental settings
Forcing deletion of images that have multiple tags
FORCE_IMAGE_REMOVAL=1
Forcing deletion of containers
FORCE_CONTAINER_REMOVAL=1
Excluding Recently Exited Containers and Images From Garbage Collection
GRACE_PERIOD_SECONDS=86400
This setting also prevents the removal of images that have been created less than GRACE_PERIOD_SECONDS seconds ago.
Dry run
DRY_RUN=1
Cleaning up orphaned container volumes
CLEAN_UP_VOLUMES=1
Reference: docker-gc
Old way to do:
delete old, non-running containers
docker ps -a -q -f status=exited | xargs --no-run-if-empty docker rm
OR
docker rm $(docker ps -a -q)
delete all images associated with non-running docker containers
docker images -q | xargs --no-run-if-empty docker rmi
cleanup orphaned docker volumes for docker version 1.10.x and above
docker volume ls -qf dangling=true | xargs -r docker volume rm
Based on time period
docker ps -a | grep "weeks ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "days ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "hours ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
1
Interesting alternative. +1
– VonC
Nov 20 '16 at 12:26
add a comment |
New way:
spotify/docker-gc play the trick.
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc spotify/docker-gc
- Containers that exited more than an hour ago are removed.
- Images that don't belong to any remaining container after that are removed
It has supported environmental settings
Forcing deletion of images that have multiple tags
FORCE_IMAGE_REMOVAL=1
Forcing deletion of containers
FORCE_CONTAINER_REMOVAL=1
Excluding Recently Exited Containers and Images From Garbage Collection
GRACE_PERIOD_SECONDS=86400
This setting also prevents the removal of images that have been created less than GRACE_PERIOD_SECONDS seconds ago.
Dry run
DRY_RUN=1
Cleaning up orphaned container volumes
CLEAN_UP_VOLUMES=1
Reference: docker-gc
Old way to do:
delete old, non-running containers
docker ps -a -q -f status=exited | xargs --no-run-if-empty docker rm
OR
docker rm $(docker ps -a -q)
delete all images associated with non-running docker containers
docker images -q | xargs --no-run-if-empty docker rmi
cleanup orphaned docker volumes for docker version 1.10.x and above
docker volume ls -qf dangling=true | xargs -r docker volume rm
Based on time period
docker ps -a | grep "weeks ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "days ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "hours ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
New way:
spotify/docker-gc play the trick.
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc spotify/docker-gc
- Containers that exited more than an hour ago are removed.
- Images that don't belong to any remaining container after that are removed
It has supported environmental settings
Forcing deletion of images that have multiple tags
FORCE_IMAGE_REMOVAL=1
Forcing deletion of containers
FORCE_CONTAINER_REMOVAL=1
Excluding Recently Exited Containers and Images From Garbage Collection
GRACE_PERIOD_SECONDS=86400
This setting also prevents the removal of images that have been created less than GRACE_PERIOD_SECONDS seconds ago.
Dry run
DRY_RUN=1
Cleaning up orphaned container volumes
CLEAN_UP_VOLUMES=1
Reference: docker-gc
Old way to do:
delete old, non-running containers
docker ps -a -q -f status=exited | xargs --no-run-if-empty docker rm
OR
docker rm $(docker ps -a -q)
delete all images associated with non-running docker containers
docker images -q | xargs --no-run-if-empty docker rmi
cleanup orphaned docker volumes for docker version 1.10.x and above
docker volume ls -qf dangling=true | xargs -r docker volume rm
Based on time period
docker ps -a | grep "weeks ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "days ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
docker ps -a | grep "hours ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
answered Nov 20 '16 at 10:51
f-societyf-society
1,4391512
1,4391512
1
Interesting alternative. +1
– VonC
Nov 20 '16 at 12:26
add a comment |
1
Interesting alternative. +1
– VonC
Nov 20 '16 at 12:26
1
1
Interesting alternative. +1
– VonC
Nov 20 '16 at 12:26
Interesting alternative. +1
– VonC
Nov 20 '16 at 12:26
add a comment |
Remove all docker processes:
docker rm $(docker ps -a -q)
Remove specific container:
$ docker ps -a (lists all old containers)
$ docker rm container-Id
add a comment |
Remove all docker processes:
docker rm $(docker ps -a -q)
Remove specific container:
$ docker ps -a (lists all old containers)
$ docker rm container-Id
add a comment |
Remove all docker processes:
docker rm $(docker ps -a -q)
Remove specific container:
$ docker ps -a (lists all old containers)
$ docker rm container-Id
Remove all docker processes:
docker rm $(docker ps -a -q)
Remove specific container:
$ docker ps -a (lists all old containers)
$ docker rm container-Id
edited Mar 10 '17 at 3:08


Peter Mortensen
13.7k1986112
13.7k1986112
answered May 15 '14 at 9:29
padakpadakpadakpadak
7125
7125
add a comment |
add a comment |
You can use the following command to remove the exited containers:
docker rm $(sudo docker ps -a | grep Exit | cut -d ' ' -f 1)
Here is the full gist to also remove the old images on docker: Gist to remove old Docker containers and images.
Practically a duplicate stackoverflow.com/a/24681946/1938621
– Ryne Everett
Jan 4 '15 at 2:09
add a comment |
You can use the following command to remove the exited containers:
docker rm $(sudo docker ps -a | grep Exit | cut -d ' ' -f 1)
Here is the full gist to also remove the old images on docker: Gist to remove old Docker containers and images.
Practically a duplicate stackoverflow.com/a/24681946/1938621
– Ryne Everett
Jan 4 '15 at 2:09
add a comment |
You can use the following command to remove the exited containers:
docker rm $(sudo docker ps -a | grep Exit | cut -d ' ' -f 1)
Here is the full gist to also remove the old images on docker: Gist to remove old Docker containers and images.
You can use the following command to remove the exited containers:
docker rm $(sudo docker ps -a | grep Exit | cut -d ' ' -f 1)
Here is the full gist to also remove the old images on docker: Gist to remove old Docker containers and images.
edited Mar 10 '17 at 3:13


Peter Mortensen
13.7k1986112
13.7k1986112
answered Oct 29 '14 at 9:50
urodozurodoz
4111
4111
Practically a duplicate stackoverflow.com/a/24681946/1938621
– Ryne Everett
Jan 4 '15 at 2:09
add a comment |
Practically a duplicate stackoverflow.com/a/24681946/1938621
– Ryne Everett
Jan 4 '15 at 2:09
Practically a duplicate stackoverflow.com/a/24681946/1938621
– Ryne Everett
Jan 4 '15 at 2:09
Practically a duplicate stackoverflow.com/a/24681946/1938621
– Ryne Everett
Jan 4 '15 at 2:09
add a comment |
The basic steps to stop/remove all containers and images
List all the containers
docker ps -aq
Stop all running containers
docker stop $(docker ps -aq)
Remove all containers
docker rm $(docker ps -aq)
Remove all images
docker rmi $(docker images -q)
Note: First you have to stop all the running containers before you remove them. Also before removing an image, you have to stop and remove its dependent container(s).
add a comment |
The basic steps to stop/remove all containers and images
List all the containers
docker ps -aq
Stop all running containers
docker stop $(docker ps -aq)
Remove all containers
docker rm $(docker ps -aq)
Remove all images
docker rmi $(docker images -q)
Note: First you have to stop all the running containers before you remove them. Also before removing an image, you have to stop and remove its dependent container(s).
add a comment |
The basic steps to stop/remove all containers and images
List all the containers
docker ps -aq
Stop all running containers
docker stop $(docker ps -aq)
Remove all containers
docker rm $(docker ps -aq)
Remove all images
docker rmi $(docker images -q)
Note: First you have to stop all the running containers before you remove them. Also before removing an image, you have to stop and remove its dependent container(s).
The basic steps to stop/remove all containers and images
List all the containers
docker ps -aq
Stop all running containers
docker stop $(docker ps -aq)
Remove all containers
docker rm $(docker ps -aq)
Remove all images
docker rmi $(docker images -q)
Note: First you have to stop all the running containers before you remove them. Also before removing an image, you have to stop and remove its dependent container(s).
edited Jul 23 '18 at 17:46


Peter Mortensen
13.7k1986112
13.7k1986112
answered Aug 4 '17 at 11:30
Murali ManchadikkalMurali Manchadikkal
33635
33635
add a comment |
add a comment |
#!/bin/bash
# docker-gc --- Remove stopped docker containers
RUNNING=$(docker ps -q)
ALL=$(docker ps -a -q)
for container in $ALL ; do
[[ "$RUNNING" =~ "$container" ]] && continue
echo Removing container: $(docker rm $container)
done
for those that don't want to search for it themselves github.com/spotify/docker-gc
– grim
Feb 24 '15 at 19:13
add a comment |
#!/bin/bash
# docker-gc --- Remove stopped docker containers
RUNNING=$(docker ps -q)
ALL=$(docker ps -a -q)
for container in $ALL ; do
[[ "$RUNNING" =~ "$container" ]] && continue
echo Removing container: $(docker rm $container)
done
for those that don't want to search for it themselves github.com/spotify/docker-gc
– grim
Feb 24 '15 at 19:13
add a comment |
#!/bin/bash
# docker-gc --- Remove stopped docker containers
RUNNING=$(docker ps -q)
ALL=$(docker ps -a -q)
for container in $ALL ; do
[[ "$RUNNING" =~ "$container" ]] && continue
echo Removing container: $(docker rm $container)
done
#!/bin/bash
# docker-gc --- Remove stopped docker containers
RUNNING=$(docker ps -q)
ALL=$(docker ps -a -q)
for container in $ALL ; do
[[ "$RUNNING" =~ "$container" ]] && continue
echo Removing container: $(docker rm $container)
done
answered May 25 '14 at 23:53
mckossmckoss
4,38442626
4,38442626
for those that don't want to search for it themselves github.com/spotify/docker-gc
– grim
Feb 24 '15 at 19:13
add a comment |
for those that don't want to search for it themselves github.com/spotify/docker-gc
– grim
Feb 24 '15 at 19:13
for those that don't want to search for it themselves github.com/spotify/docker-gc
– grim
Feb 24 '15 at 19:13
for those that don't want to search for it themselves github.com/spotify/docker-gc
– grim
Feb 24 '15 at 19:13
add a comment |
I always use docker rmi $(docker ps -a -q)
to remove all images.
You can remove directory /var/lib/docker/graph
when docker rmi
failed.
Thanks for adding the /var/lib/docker/graph info
– Jonathan DeMarks
Mar 5 '15 at 15:26
The questions was how to remove containers, not images.
– pauk960
Mar 16 '15 at 19:34
add a comment |
I always use docker rmi $(docker ps -a -q)
to remove all images.
You can remove directory /var/lib/docker/graph
when docker rmi
failed.
Thanks for adding the /var/lib/docker/graph info
– Jonathan DeMarks
Mar 5 '15 at 15:26
The questions was how to remove containers, not images.
– pauk960
Mar 16 '15 at 19:34
add a comment |
I always use docker rmi $(docker ps -a -q)
to remove all images.
You can remove directory /var/lib/docker/graph
when docker rmi
failed.
I always use docker rmi $(docker ps -a -q)
to remove all images.
You can remove directory /var/lib/docker/graph
when docker rmi
failed.
edited Mar 10 '17 at 3:07


Peter Mortensen
13.7k1986112
13.7k1986112
answered Jan 6 '14 at 14:34


sunnysunny
392
392
Thanks for adding the /var/lib/docker/graph info
– Jonathan DeMarks
Mar 5 '15 at 15:26
The questions was how to remove containers, not images.
– pauk960
Mar 16 '15 at 19:34
add a comment |
Thanks for adding the /var/lib/docker/graph info
– Jonathan DeMarks
Mar 5 '15 at 15:26
The questions was how to remove containers, not images.
– pauk960
Mar 16 '15 at 19:34
Thanks for adding the /var/lib/docker/graph info
– Jonathan DeMarks
Mar 5 '15 at 15:26
Thanks for adding the /var/lib/docker/graph info
– Jonathan DeMarks
Mar 5 '15 at 15:26
The questions was how to remove containers, not images.
– pauk960
Mar 16 '15 at 19:34
The questions was how to remove containers, not images.
– pauk960
Mar 16 '15 at 19:34
add a comment |
You can remove only stopped containers. Stop all of them in the beginning
docker stop $(docker ps -a -q)
Then you can remove
docker rm $(docker ps -a -q)
add a comment |
You can remove only stopped containers. Stop all of them in the beginning
docker stop $(docker ps -a -q)
Then you can remove
docker rm $(docker ps -a -q)
add a comment |
You can remove only stopped containers. Stop all of them in the beginning
docker stop $(docker ps -a -q)
Then you can remove
docker rm $(docker ps -a -q)
You can remove only stopped containers. Stop all of them in the beginning
docker stop $(docker ps -a -q)
Then you can remove
docker rm $(docker ps -a -q)
answered May 24 '17 at 19:11
Marcin SzymczakMarcin Szymczak
7,00333148
7,00333148
add a comment |
add a comment |
Try this command to clean containers and dangling images.
docker system prune -f
This is an amazing pro-tip!
– b01
Jun 15 '18 at 18:05
add a comment |
Try this command to clean containers and dangling images.
docker system prune -f
This is an amazing pro-tip!
– b01
Jun 15 '18 at 18:05
add a comment |
Try this command to clean containers and dangling images.
docker system prune -f
Try this command to clean containers and dangling images.
docker system prune -f
answered Feb 8 '18 at 21:58


PalPal
44025
44025
This is an amazing pro-tip!
– b01
Jun 15 '18 at 18:05
add a comment |
This is an amazing pro-tip!
– b01
Jun 15 '18 at 18:05
This is an amazing pro-tip!
– b01
Jun 15 '18 at 18:05
This is an amazing pro-tip!
– b01
Jun 15 '18 at 18:05
add a comment |
To remove ALL containers without crying:
sudo docker ps -a | grep -v CONTAINER | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
add a comment |
To remove ALL containers without crying:
sudo docker ps -a | grep -v CONTAINER | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
add a comment |
To remove ALL containers without crying:
sudo docker ps -a | grep -v CONTAINER | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
To remove ALL containers without crying:
sudo docker ps -a | grep -v CONTAINER | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f
answered Feb 6 '15 at 3:11
FelipeFelipe
10.9k75278
10.9k75278
add a comment |
add a comment |
For anyone interested, I took the example from qkrijger and turned it into a clear all (stop and remove all)
docker stop `docker ps --no-trunc -aq` ; docker rm `docker ps --no-trunc -aq`
If you are removing the containers anyway, thendocker rm -f $(docker ps --no-trunc -aq)
is faster
– qkrijger
Apr 10 '15 at 7:50
True, but if you need it to go through proper shutdown to cleanup shared or reused resources (i.e. Attached volumes, correctly closed active connections, etc) then you need to call stop first. Great example, if you have a Jenkins container, then killing the container is a bad idea. You want it to save any unpersisted changes, warn logged in users, and, very importantly, properly stop all builds.
– Kraig McConaghy
Apr 10 '15 at 12:57
agreed, that is a very valid use case
– qkrijger
Apr 26 '15 at 22:55
add a comment |
For anyone interested, I took the example from qkrijger and turned it into a clear all (stop and remove all)
docker stop `docker ps --no-trunc -aq` ; docker rm `docker ps --no-trunc -aq`
If you are removing the containers anyway, thendocker rm -f $(docker ps --no-trunc -aq)
is faster
– qkrijger
Apr 10 '15 at 7:50
True, but if you need it to go through proper shutdown to cleanup shared or reused resources (i.e. Attached volumes, correctly closed active connections, etc) then you need to call stop first. Great example, if you have a Jenkins container, then killing the container is a bad idea. You want it to save any unpersisted changes, warn logged in users, and, very importantly, properly stop all builds.
– Kraig McConaghy
Apr 10 '15 at 12:57
agreed, that is a very valid use case
– qkrijger
Apr 26 '15 at 22:55
add a comment |
For anyone interested, I took the example from qkrijger and turned it into a clear all (stop and remove all)
docker stop `docker ps --no-trunc -aq` ; docker rm `docker ps --no-trunc -aq`
For anyone interested, I took the example from qkrijger and turned it into a clear all (stop and remove all)
docker stop `docker ps --no-trunc -aq` ; docker rm `docker ps --no-trunc -aq`
answered Apr 9 '15 at 6:13
Kraig McConaghyKraig McConaghy
21834
21834
If you are removing the containers anyway, thendocker rm -f $(docker ps --no-trunc -aq)
is faster
– qkrijger
Apr 10 '15 at 7:50
True, but if you need it to go through proper shutdown to cleanup shared or reused resources (i.e. Attached volumes, correctly closed active connections, etc) then you need to call stop first. Great example, if you have a Jenkins container, then killing the container is a bad idea. You want it to save any unpersisted changes, warn logged in users, and, very importantly, properly stop all builds.
– Kraig McConaghy
Apr 10 '15 at 12:57
agreed, that is a very valid use case
– qkrijger
Apr 26 '15 at 22:55
add a comment |
If you are removing the containers anyway, thendocker rm -f $(docker ps --no-trunc -aq)
is faster
– qkrijger
Apr 10 '15 at 7:50
True, but if you need it to go through proper shutdown to cleanup shared or reused resources (i.e. Attached volumes, correctly closed active connections, etc) then you need to call stop first. Great example, if you have a Jenkins container, then killing the container is a bad idea. You want it to save any unpersisted changes, warn logged in users, and, very importantly, properly stop all builds.
– Kraig McConaghy
Apr 10 '15 at 12:57
agreed, that is a very valid use case
– qkrijger
Apr 26 '15 at 22:55
If you are removing the containers anyway, then
docker rm -f $(docker ps --no-trunc -aq)
is faster– qkrijger
Apr 10 '15 at 7:50
If you are removing the containers anyway, then
docker rm -f $(docker ps --no-trunc -aq)
is faster– qkrijger
Apr 10 '15 at 7:50
True, but if you need it to go through proper shutdown to cleanup shared or reused resources (i.e. Attached volumes, correctly closed active connections, etc) then you need to call stop first. Great example, if you have a Jenkins container, then killing the container is a bad idea. You want it to save any unpersisted changes, warn logged in users, and, very importantly, properly stop all builds.
– Kraig McConaghy
Apr 10 '15 at 12:57
True, but if you need it to go through proper shutdown to cleanup shared or reused resources (i.e. Attached volumes, correctly closed active connections, etc) then you need to call stop first. Great example, if you have a Jenkins container, then killing the container is a bad idea. You want it to save any unpersisted changes, warn logged in users, and, very importantly, properly stop all builds.
– Kraig McConaghy
Apr 10 '15 at 12:57
agreed, that is a very valid use case
– qkrijger
Apr 26 '15 at 22:55
agreed, that is a very valid use case
– qkrijger
Apr 26 '15 at 22:55
add a comment |
docker rm --force `docker ps -qa`
add a comment |
docker rm --force `docker ps -qa`
add a comment |
docker rm --force `docker ps -qa`
docker rm --force `docker ps -qa`
answered Oct 24 '16 at 20:57
SivalingamSivalingam
452610
452610
add a comment |
add a comment |
Remove all containers created from a certain image:
docker rm $(docker ps -a | awk '/myimage:mytag/{print $1}')
Nice, I like it :D
– qkrijger
May 20 '14 at 8:37
add a comment |
Remove all containers created from a certain image:
docker rm $(docker ps -a | awk '/myimage:mytag/{print $1}')
Nice, I like it :D
– qkrijger
May 20 '14 at 8:37
add a comment |
Remove all containers created from a certain image:
docker rm $(docker ps -a | awk '/myimage:mytag/{print $1}')
Remove all containers created from a certain image:
docker rm $(docker ps -a | awk '/myimage:mytag/{print $1}')
edited Mar 10 '17 at 3:09


Peter Mortensen
13.7k1986112
13.7k1986112
answered May 19 '14 at 13:38
estaniestani
8,95224534
8,95224534
Nice, I like it :D
– qkrijger
May 20 '14 at 8:37
add a comment |
Nice, I like it :D
– qkrijger
May 20 '14 at 8:37
Nice, I like it :D
– qkrijger
May 20 '14 at 8:37
Nice, I like it :D
– qkrijger
May 20 '14 at 8:37
add a comment |
1 2
next
protected by Baum mit Augen Aug 25 '17 at 20:22
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
21
You should also consider cleaning orphaned docker volumes. I often find that they consume much more space than old containers and old images. Good script for removing orphaned docker volumes is available at: github.com/chadoe/docker-cleanup-volumes.
– Nemanja Trifunovic
Dec 1 '15 at 18:06
Maybe github.com/chadoe/docker-cleanup-volumes can help you.
– user1929959
Sep 13 '16 at 19:34
You can also use
docker run
with the--rm
flag which would make the container ephemeral, removing all container files after the run.– Gordon
Sep 21 '16 at 5:51
12
With docker 1.13 (Q4 2016), you can also consider the new
docker system prune
command. See my answer below.– VonC
Oct 4 '16 at 19:52
1
Use the docker management tool
Portainer
We can manageall the old containers, non using volumes and images
by using this tool Its a simple management UI for dockersPlease refer my update below on how to deploy the application
– Anish Varghese
Nov 22 '18 at 4:47