13

I want to remove all of my docker containers at once. I tried to use $ docker rm [container_id] to do so, but it removed only one container, not all.

Is there any way to remove all docker containers using one single line of code?

11 Answers 11

12

Remove containers based on status:

docker rm -v $(docker ps --filter status=exited -q)

Note:

  • The "-v" option that will delete any volumes associated with the containers.

To clean out all containers on my development machine:

docker rm -v -f $(docker ps -qa)

Note:

  • The "-f" option will force the removal of a running container
2
  • I would suggest adding --filter status=dead too. It doesn't come up often, but can. Also, would suggest including docker rmi $(docker images --dangling=true -q) to clean up any images that aren't in use any more.
    – Sobrique
    Feb 6, 2016 at 15:20
  • Stop All container | $ docker stop $(docker ps -a -q) and Remove All containers | $ docker rm $(docker ps -a -q)
    – Jinna Balu
    Jan 29, 2017 at 14:48
3
docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)
2
  • Wouldn't do this. It'll delete any storage containers you've created.
    – Sobrique
    Feb 6, 2016 at 15:22
  • The question asked how to "remove all docker containers". Ie. including storage containers Aug 14, 2016 at 1:13
3

For Windows:

C:\> for /F %i in ('docker ps -qa') do docker rm %i
3

For Windows (PowerShell):

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

As from Docker 1.13.0 --- Docker API 1.25:

docker container prune

Output:

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
df226cc24539833a1c88f46bfa382ebe2e89c21805288f5e6bfc37cb7f662505
993bd58faaa22cb5dbc263eca33c8d1a241bd0a1f73b082e23e3a249fe1dfc0d
...

Total reclaimed space: 13.88MB

See more on docker container prune

There is docker versioning matrix for better understanding docker versions (current is Docker 17.12 --- Docker API 1.35)

1

on Ubuntu

sudo docker ps -qa | xargs -n1 sudo docker rm
0
1

I can show you an amazing trick which helps you to be a better Linux user. by typing docker ps -a | cut -d ' ' -f1 | xargs docker rm all containers will be removed. first, you list all docker containers, then separate all container ids then, remove all of them in one line of command.

0

i do it with a bash script loop and a docker rm command:

$ for id in $(docker ps -aq); do docker rm $id; done
0

Killing Active Containers: for /F %i in ('docker ps') do docker kill %i

Remove Passive Containers: for /F %i in ('docker ps -qa') do docker rm %i

It is works in Docker 17.xx

0

These two commands if you want to remove all container or image

for container

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

for images

sudo docker rm -f $(sudo docker images -a -q)

by using -f flag we can remove the container forcefully

-1

You can use the following line

docker kill $(docker ps -q) || true && docker rm $(docker ps -a -q) || true && docker rmi -f $(docker images -q) || true

If you are using jenkins or Gitlb CI/CD, just running docker rm $(docker ps -a -q) or any other relevant command would fail if there are no containers or images. With the above line, it will be prevented.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.