Erfan Tahriri
Erfan Tahriri

Reputation: 641

How to remove all Docker containers

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?

Upvotes: 50

Views: 156023

Answers (11)

Vipin Pandey
Vipin Pandey

Reputation: 907

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 image remove -f $(sudo docker images -a -q)

by using -f flag we can remove the container forcefully

Upvotes: 15

Salman
Salman

Reputation: 126

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.

Upvotes: 2

Jananath Banuka
Jananath Banuka

Reputation: 3873

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.

Upvotes: -1

David
David

Reputation: 2252

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)

Upvotes: 31

Mark O'Connor
Mark O'Connor

Reputation: 77961

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

Upvotes: 91

celikgumusdag
celikgumusdag

Reputation: 45

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

Upvotes: 0

Lachie White
Lachie White

Reputation: 1251

For Windows (PowerShell):

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

Upvotes: 3

heronsanches
heronsanches

Reputation: 596

on Ubuntu

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

Upvotes: 3

densom
densom

Reputation: 725

For Windows:

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

Upvotes: 5

mainframer
mainframer

Reputation: 22069

docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)

Upvotes: 3

Erfan Tahriri
Erfan Tahriri

Reputation: 641

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

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

Upvotes: 1

Related Questions