188

How can I stop and remove all docker containers to create a clean slate with my Docker containers? Lots of times I feel it is easier to start from scratch, but I have a bunch of containers that I am not sure what their states are, then when I run docker rm it won't let me because the docker container could still be in use.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
jjbskir
  • 8,474
  • 9
  • 40
  • 53
  • Possible duplicate of [How to remove old Docker containers](https://stackoverflow.com/questions/17236796/how-to-remove-old-docker-containers) – DarkCygnus Jul 27 '17 at 17:53
  • Yea, I am not sure if it's a duplicate or not. That question you posted is about deleting old containers, while what I want to know is how to just delete everything, regardless of how old it is. But there is def overlap. – jjbskir Jul 27 '17 at 18:22
  • The question linked sure asks for old containers but the solution applies to ANY container you specify. So you could adapt that other solution to your case – DarkCygnus Jul 27 '17 at 18:38

13 Answers13

353

Stop all the containers

docker stop $(docker ps -a -q)

Remove all the containers

docker rm $(docker ps -a -q)

Find more command here

John Kuriakose
  • 4,065
  • 2
  • 13
  • 20
82

Docker introduced new namespaces and commands which everyone should finally learn and not stick to the old habits. Here is the documentation, and here are some examples:

Deleting no longer needed containers (stopped)

docker container prune

Deleting no longer needed images

which means, that it only deletes images, which are not tagged and are not pointed on by "latest" - so no real images you can regularly use are deleted

docker image prune

Delete all volumes, which are not used by any existing container

( even stopped containers do claim volumes ). This usually cleans up dangling anon-volumes of containers have been deleted long time ago. It should never delete named volumes since the containers of those should exists / be running. Be careful, ensure your stack at least is running before going with this one

docker volume prune

Same for unused networks

docker network prune

And finally, if you want to get rid if all the trash - to ensure nothing happens to your production, be sure all stacks are running and then run

docker system prune
kjones
  • 1,339
  • 1
  • 13
  • 28
Eugen Mayer
  • 8,942
  • 4
  • 33
  • 57
  • 2
    `docker system prun` towards last is missing an `e` – Ayushya Aug 03 '17 at 20:15
  • 2
    IMO `docker system prune` is the real answer to this question – ndm13 Dec 16 '20 at 01:27
  • 2
    Although this is useful, none of these commands allow to stop all running containers, just clean up what already stopped containers left. So first you still need to stick to the "old habits" and execute `docker stop $(docker ps -q)` to stop them all gracefully, or `docker kill $(docker ps -q)` if there are some "reveld" containers that don't want to be stopped. – Mariano Ruiz Mar 27 '23 at 18:54
17
docker ps -aq | xargs docker stop | xargs docker rm

or

docker ps -aq | xargs docker rm -f
Andriy Tolstoy
  • 5,690
  • 2
  • 31
  • 30
  • 3
    Kindly provide brief details of the docker commands you mentioned for the users. Details are always helpful especially for students and junior developers. – Muhammad Tariq Jan 13 '21 at 10:07
  • This command kills everything. Nice for those who are in a hurry. – Elias Prado Nov 19 '22 at 00:07
  • Note that this will throw an error if there are NO containers e.g. a deploy script run on a fresh instance. One would need to add a check. – alphazwest Feb 20 '23 at 14:19
  • 1
    Added `-r` like below to stop containers without error if docker ps -aq returns empty: docker ps -aq | xargs -r docker stop – Gloorfindel Feb 20 '23 at 15:16
15

One command line for cleaning all containers

docker system prune -f ; docker volume prune -f ;docker rm -f -v $(docker ps -q -a)
Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
7

The one liner:

docker rm -f $(docker ps -a -q)

If you only want to do the running ones, remove -a.

Travis Reeder
  • 38,611
  • 12
  • 87
  • 87
7

We can achieve this with one liner (also removes running containers)

docker container rm $(docker container ls -aq) -f

What it does

docker container ls -aq lists container's ids only

docker container rm $(..) -f forcibly removes all container's ids

Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
6

Here's a good gist I use for this kind of thing: From this link that people seem to not like (https://gist.github.com/bastman/5b57ddb3c11942094f8d0a97d461b430)

delete volumes

$ docker volume rm $(docker volume ls -qf dangling=true)
$ docker volume ls -qf dangling=true | xargs -r docker volume rm

delete networks

$ docker network ls  
$ docker network ls | grep "bridge"   
$ docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }')

remove docker images

// see: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images

$ docker images
$ docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
$ docker images | grep "none"
$ docker rmi $(docker images | grep "none" | awk '/ / { print $3 }')

remove docker containers

// see: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images

$ docker ps
$ docker ps -a
$ docker rm $(docker ps -qa --no-trunc --filter "status=exited")

Essentially you want to kill all your running containers, remove every image, uninstall docker, reinstall the version you want and that should be about as clean a slate as it gets.

Chris
  • 377
  • 1
  • 7
  • 1
    Could you please post the commands instead of just a link to the gist? This will greatly help those users that can not follow your link to benefit from your answer. Also, some day that link may become broken and with it your answer become less usefull – DarkCygnus Jul 27 '17 at 17:54
  • Thank you, removed my DV – DarkCygnus Jul 27 '17 at 18:00
  • 1
    It's all good. Just trying to give back to the community, I've never actually answered these before. I guess all devs feel the call at some point? – Chris Jul 27 '17 at 18:12
  • That is great @Chris helping others is really honorable, as well as a good opportunity to reinforce your knowledge in the subject, as writing answers some times leads you to learn more on that theme or clear some old doubts you had. Just have in mind that your answers should be clear and consider aspects like this, which are required for good answers in SO – DarkCygnus Jul 27 '17 at 18:18
  • downvoted since massively outdated. There is no need for filter dangling=true any, prune is here – Eugen Mayer Jul 27 '17 at 19:22
4

If you are concerned about shellcheck SC2046 (in which case you would receive a warning for the command docker stop $(docker ps -a -q)) and you are using Bash4+ (to be able to use the mapfile builtin command), you can run the following to stop all containers:

mapfile -t list < <(docker ps -q)
[[ ${#list[@]} -gt 0 ]] && docker container stop "${list[@]}"

The reasoning:

  • docker ps -q returns all active containers ids

  • mapfile stores the resulting container ids in the list array

  • [[ ${#list[@]} -gt 0 ]] tests if the list array has 1 or more elements to execute the next command

  • docker container stop "${list[@]}" stops all containers whose ids are stored in the listarray (will only run if the array has items)

Similarly, to remove all stopped containers:

mapfile -t list < <(docker ps -aq)
[[ ${#list[@]} -gt 0 ]] && docker container rm "${list[@]}"

(docker ps -aq returns all container ids, even from stopped containers)

If you want to stop and remove all containers, you can run the above commands sequentially (first stop, then rm).

Alternatively, you can run only the the commands to remove the containers with rm --force, but keep in mind that this will send SIGKILL, so running stopfirst and then rm is advisable (stop sends SIGTERM, unless it times out, in which case it sends SIGKILL).

Lucas Basquerotto
  • 7,260
  • 2
  • 47
  • 61
  • that's to accomodate spaces in container names? – kjones Mar 17 '21 at 04:42
  • 1
    @kjones If you use shellcheck you receive a warning with unquotted `$()` because "When command expansions are unquoted, word splitting and globbing will occur". In the case of docker container ids there's no such problem, so you can just disable the rule, but if you don't want to disable the rule, you can use the alternative way I posted. It does the same thing, although there is more code, so you might prefer just `docker stop $(docker ps -a -q)`, but I try to avoid unescaped quotes if there is an alternative, even if I know that there's no problem in this specific case. – Lucas Basquerotto Mar 17 '21 at 12:32
  • i see, thank you for the follow up explanation – kjones Mar 17 '21 at 16:33
4

stop containers:

docker stop container1_id container2_id containerz_id 

delete all image:

docker system prune --all
Rashid Iqbal
  • 1,123
  • 13
  • 13
0

then when I run docker rm it won't let me because the docker container could still be in use.

The steps I would suggest are in following order,
1. Try to remove

docker rm <container-id>

2. rm doesn't work, use stop

docker stop <container-id>

3. stop doesn't work? try kill

docker kill <container-id>

4. stop worked but still container is there? try prune to remove all the stopped container forcefully

docker container prune -f
Pritesh Gohil
  • 416
  • 7
  • 15
0

To remove all Docker images:

docker rmi $(docker images -aq)
Antoine
  • 1,393
  • 4
  • 20
  • 26
vidya
  • 1
0

An answer on the spirit of Pankaj Rastogi's comment, in one of the answers.

  1. create a list of all containers and store them in a text file

docker ps > containers.txt

  1. pass the CONTAINER IDs to docker rm one by one.

cat containers.txt | tail -n +2 | cut -d ' ' -f 1 | xargs -I{} docker rm {}.

The latter lists all container entries, which start with a container id. Entries are separated by space and therefore, tail -n +2 skips the first line which is headers, cut -d ' ' -f 1, splits every line by space and takes the first line entry, which is the container id and xargs -I{} docker rm {} removes the container with the specific id.

NOTES:

  • the above works also for udocker.
  • these commands, remove all images. If you want to inly remove some images, you can perhaps use a grep step, a bit before xargs, in order to filter out some of them.
Yannis P.
  • 2,745
  • 1
  • 24
  • 39
-1

In ubuntu I just killed all processes when other solutions didn't work.

$ ps aux | grep docker
$ sudo kill {enter process id here}

I do not recommend doing this way.I was on reinstalling your OS, when these command saved some time for fixing my issues with containers.

Aipo
  • 1,805
  • 3
  • 23
  • 48
  • 2
    of if you want to stop/remove matching containers: docker ps | grep 'localstack\|postgres' | awk '{print $1}' | xargs docker rm -f – Pankaj Rastogi Apr 20 '21 at 11:16