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?


Since Docker 1.13.x you can use Docker container prune:

docker container prune

This will remove all stopped containers and should work on all platforms the same way.

There is also a Docker system prune:

docker system prune

which will clean up all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes, in one command.


For older Docker versions, 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.


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

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)

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.


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