20

I remember using

docker rm -f `docker ps -aq`

to chain the commands without an issue a few months ago, but now this isn't working, and I'm getting the following output:

unknown shorthand flag: 'a' in -aq`
See 'docker rm --help'.

What changed? How can I delete all Docker running containers in one line? In case it helps, I'm using Docker for Windows (native with Hyper-V, not with VirtualBox) on Windows 10, and the command I used has worked fine with my previous Windows 8 Docker toolbox installation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sgarcia.dev
  • 5,671
  • 14
  • 46
  • 80

12 Answers12

28

Till now (Docker version 1.12) we are using the following command to delete all the running containers (also, if we want to delete the volumes, we can do that manually using its respective tag -v in the following command),

Delete all Exited Containers

docker rm $(docker ps -q -f status=exited)

Delete all Stopped Containers

docker rm $(docker ps -a -q)

Delete All Running and Stopped Containers

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

Remove all containers, without any criteria

docker container rm $(docker container ps -aq)

But, in version 1.13 and above, for complete system and cleanup, we can directly user the following command,

docker system prune

All unused containers, images, networks and volumes will get deleted. Also, individually i.e. separately, we can do that using the following commands, that clean up the components,

docker container prune
docker image prune
docker network prune
docker volume prune
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mohan08p
  • 5,002
  • 1
  • 28
  • 36
8

I had this issue when running in cmd. Switched to PowerShell and it worked!

D-Go
  • 413
  • 5
  • 9
6

Use:

docker rm -f $(docker ps -aq)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fazni
  • 61
  • 1
  • 2
4

If anybody needs the Windows Shell Command (stop and remove container), here it is:

for /F %c in ('docker ps -a -q') do (docker stop %c)
for /F %c in ('docker ps -a -q') do (docker rm %c)

If you put it in a batch file, just add % to %c:

for /F %%c in ('docker ps -a -q') do (docker stop %%c)
swissben
  • 1,059
  • 8
  • 13
1

I've had the same problem: I was on a Windows machine and used Docker within VirtualBox and the command docker rm -f ${docker ps -aq} worked well. Then I switched to Docker for Windows and the command didn't work on the Windows command line.

But using Cygwin or Git Bash solved the problem for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Munchkin
  • 4,528
  • 7
  • 45
  • 93
1

Try using this command.

docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')
Shahbaz A.
  • 4,047
  • 4
  • 34
  • 55
iamsj
  • 84
  • 13
1

Run docker commands in Windows PowerShell will execute and run most of the commands

Hope you also remember to stop running containers first before running the delete command

docker stop $(docker ps -aq)  
Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
Ndiklas
  • 77
  • 1
  • 6
1
$ docker rm $(docker ps --filter status=created -q)

Tested on Docker version 19.03.5, build 633a0ea on Mac OS Mojave.

user674669
  • 10,681
  • 15
  • 72
  • 105
0

Use this:

docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')

If you want to include previously stopped containers:

docker rm -f $(docker ps -a | grep -v CONTAINER | awk '{print $1}')
nPcomp
  • 8,637
  • 2
  • 54
  • 49
0

If the container is running, you cannot delete the image. First stop all the containers using the following command.

docker stop $(docker ps -aq)

you are saying running stop against the output of docker ps -aq. 'a' - get me all the containers 'q' - return only the container id.

Then run the following command to remove all the containers.

docker rm $(docker ps -aq)
Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
Keerthikanth Chowdary
  • 728
  • 1
  • 10
  • 17
0

Single command to delete all stop and running containers (first stop and then just prune/remove them. Works for me all the time.

docker stop $(docker ps -a -q) && docker container prune -a
Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
rebelution
  • 389
  • 2
  • 13
0
  1. we can delete all running containers in docker ENV by the following the command -
docker container rm -f $(docker container ls -aq)

It should to the magic

  1. if we have run our docker container using docker-compose.yaml then
docker-compose -f /path/to/compose/file down

should work

Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33