[docker] 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?

This question is related to docker

The answer is


New way: spotify/docker-gc play the trick.

 docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc spotify/docker-gc
  • Containers that exited more than an hour ago are removed.
  • Images that don't belong to any remaining container after that are removed

It has supported environmental settings

Forcing deletion of images that have multiple tags

 FORCE_IMAGE_REMOVAL=1

Forcing deletion of containers

 FORCE_CONTAINER_REMOVAL=1 

Excluding Recently Exited Containers and Images From Garbage Collection

 GRACE_PERIOD_SECONDS=86400

This setting also prevents the removal of images that have been created less than GRACE_PERIOD_SECONDS seconds ago.

Dry run

 DRY_RUN=1

Cleaning up orphaned container volumes CLEAN_UP_VOLUMES=1

Reference: docker-gc

Old way to do:

delete old, non-running containers

 docker ps -a -q -f status=exited | xargs --no-run-if-empty docker rm
             OR 
 docker rm $(docker ps -a -q)

delete all images associated with non-running docker containers

 docker images -q | xargs --no-run-if-empty docker rmi

cleanup orphaned docker volumes for docker version 1.10.x and above

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

Based on time period

 docker ps -a | grep "weeks ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
 docker ps -a | grep "days ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm
 docker ps -a | grep "hours ago" | awk "{print $1}" | xargs --no-run-if-empty docker rm

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

I am using following commands to delete Exited and Restarting docker containers

docker stop --force $(docker ps -a|grep Exited| awk '{print $1}')
docker rm --force $(docker ps -a|grep Exited| awk '{print $1}')
docker stop --force $(docker ps -a|grep Restarting| awk '{print $1}')
docker rm --force $(docker ps -a|grep Restarting| awk '{print $1}')

Using below command to remove images named as none

docker image rm --force $(docker image ls  |grep none |awk '{print $3}')

For a Linux installation make sure you use sudo. Also it's good to look for images that are months and weeks old:

sudo docker ps -a | grep 'weeks ago\|months ago' | \
    awk '{print $1}' | xargs --no-run-if-empty sudo docker rm

  1. Remove all docker processes:

    docker rm $(docker ps -a -q)
    
  2. Remove specific container:

    $ docker ps -a (lists all old containers)
    
    $ docker rm container-Id
    

To get rid of your stopped container you can use:

docker rm <cointainer_id>

You can also use the name of the container:

docker rm <name>

If you want to get rid of all the stopped containers you can use the:

docker container prune

Or you can also use:

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

You can use -v argument to delete any docker managed volumes that are not referenced any further

docker rm -v $(docker ps -aq -f status=exited)

You can also use --rm with the docker run. This will delete the container and the associated files when the container exists.


I'm using:

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

to delete exited containers and:

docker rmi -f $(docker images | grep "<none>" | awk "{print \$3}")

in order to get rid of all untagged images.


Here is a one-liner that removes all the exited containers.

docker rm $(docker ps -a | grep Exited | grep -v CON | awk '{print $1}')

If you want to remove ALL the images you can use something like this.

docker rmi $(docker images | sed -n '1!p' | awk '{print $3}')


To delete a specify container

docker container rm container_id

If the container is running, you have to stop it before to delete it

docker container stop container_id

And this command is to delete all existing containers

docker container rm $(docker container -a -q)

UPDATED 2021 (NEWEST)

docker container prune

This - 2017 (OLD) way

To remove ALL STOPPED CONTAINERS

docker rm $(docker ps -a -q)

To remove ALL CONTAINERS (STOPPED AND NON STOPPED)

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

This short script might help (compiled from previous answers)!

#!/bin/bash

# Remove dangling images

IMAGE_IDS=$(sudo docker images -f "dangling=true" -q)

if [ -n "$IMAGE_IDS" ]; then

    sudo docker rmi $IMAGE_IDS > /dev/null 2>&1
    echo 'Images removed' $IMAGE_IDS
fi


# Remove exited containers

CONTAINER_IDS=$(sudo docker ps -a -q -f status=exited)

if [ -n "$CONTAINER_IDS" ]; then

    sudo docker rm -v $CONTAINER_IDS > /dev/null 2>&1
    echo 'Containers remove $CONTAINER_IDS'
fi

docker rm -f $(docker ps -a -q) will do the trick. It will stop the containers and remove them too.


Remove all stopped containers.

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

This will remove all stopped containers by getting a list of all containers with docker ps -a -q and passing their ids to docker rm. This should not remove any running containers, and it will tell you it can’t remove a running image.

Remove all untagged images

Now you want to clean up old images to save some space.

sudo docker rmi $(sudo docker images -q --filter "dangling=true")


For anyone interested, I took the example from qkrijger and turned it into a clear all (stop and remove all)

docker stop `docker ps --no-trunc -aq` ; docker rm `docker ps --no-trunc -aq`

To simply remove everything that is not currently used by a running container the following alias, that I usually put into the .bash_profile on my Mac, will help:

alias dockerclean="docker ps -q -a | xargs docker rm -v && docker images -q | xargs docker rmi"

Whenever dockerclean is invoked from the command line it will remove stopped containers as well as unused image layers. For running containers and used images it will print a warning message and skip over it.


You can remove only stopped containers. Stop all of them in the beginning

docker stop $(docker ps -a -q)

Then you can remove

docker rm $(docker ps -a -q)


You can use docker-helper from the repository https://github.com/kartoza/docker-helpers. After the install, just type drmc.


Use the following nested commands:

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

This command stops all running containers.

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

This command remove all containers.


It is now possible to use filtering with docker ps:

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

And for images:

docker rmi $(docker images -q -f "dangling=true")

However, any of those will cause docker rm or docker rmi to throw an error when there are no matching containers. The older docker rm $(docker ps -aq) trick was even worse as it tried to remove any running container, failing at each one.

Here's a cleaner script to add in your ~/.bashrc or ~/.profile :

# Use `docker-cleanup --dry-run` to see what would be deleted.

function docker-cleanup {
  EXITED=$(docker ps -q -f status=exited)
  DANGLING=$(docker images -q -f "dangling=true")

  if [ "$1" == "--dry-run" ]; then
    echo "==> Would stop containers:"
    echo $EXITED
    echo "==> And images:"
    echo $DANGLING
  else
    if [ -n "$EXITED" ]; then
      docker rm $EXITED
    else
      echo "No containers to remove."
    fi
    if [ -n "$DANGLING" ]; then
      docker rmi $DANGLING
    else
      echo "No images to remove."
    fi
  fi
}

Edit: As noted below, original answer was for removing images, not containers. Updated to answer both, including new links to documentation. Thanks to Adrian (and Ryan's answer) for mentioning the new ps filtering.


To remove ALL stopped docker containers, run:-

$ docker container prune

You'll have to be careful with this command since it removes all stopped containers indiscriminately so make sure there's no stopped/currently unused container that you may still have use of.

A more precise alternative is to remove the container by ID. You'll have to first list the stopped containers using this command:-

docker ps --filter "status=exited"

You'll then copy the ID of the container you want to remove. Thereafter, execute the following command that removes a single container,

docker container rm <container ID>

or the one below to remove multiple containers at once by running:-

docker container rm <container ID 1> <container ID 2> <container ID n>

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

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)

#!/bin/bash

echo Cleanup unused containers
unused_containers=$(docker ps -a -q --filter="status=exited")
if [[ -n $unused_containers ]]
then
  docker rm $unused_containers
fi

echo Cleanup unused images
unused_images=$(docker images | grep '^<none>' | awk '{print $3}')
if [[ -n $unused_images ]]
then
  docker rmi $unused_images
fi

You can use some of the Docker UI applications to remove containers.

Sorry for advertisement, but I always use my own application to do the same things. You can try it if you are looking for a simple application to manage Docker images or containers: https://github.com/alex-agency/AMHub.

This is a Docker UI web application which is running inside a Docker container. For installing it, you only need invoke this command:

docker run -d -p 80:80 -p 8000:8000 -e DOCKER=$(which docker) -v /var/run/docker.sock:/docker.sock alexagency/amhub

Use the docker management tool Portainer

We can manage all the old containers, non using volumes and images by using this tool

Its a simple management UI for dockers

HOW TO DEPLOY PORTAINER

Use the following Docker commands to deploy Portainer:

$ docker volume create portainer_data
$ docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

You'll just need to access the port 9000 of the Docker engine where portainer is running using your browser.

Note: the -v /var/run/docker.sock:/var/run/docker.sock option can be used in Linux environments only.


docker rm --force `docker ps -qa`

I always use docker rmi $(docker ps -a -q) to remove all images.

You can remove directory /var/lib/docker/graph when docker rmi failed.


Remove 5 oldest containers:

docker rm `docker ps -aq | tail -n 5`

See how many containers there are left:

docker ps -aq | wc -l

The basic steps to stop/remove all containers and images

  1. List all the containers

    docker ps -aq

  2. Stop all running containers

    docker stop $(docker ps -aq)

  3. Remove all containers

    docker rm $(docker ps -aq)

  4. Remove all images

    docker rmi $(docker images -q)

Note: First you have to stop all the running containers before you remove them. Also before removing an image, you have to stop and remove its dependent container(s).


If you want to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download the image meltwater/docker-cleanup.

I use this in production since we deploy several times a day on multiple servers, and I don't want to go to every server to clean up (that would be a pain).

Just run:

docker run -d -v /var/run/docker.sock:/var/run/docker.sock:rw  -v /var/lib/docker:/var/lib/docker:rw --restart=unless-stopped meltwater/docker-cleanup:latest

It will run every 30 minutes by default (or however long you set it using DELAY_TIME=1800 option) and clean up exited containers and images.

More details: Docker Cleanup


List all containers (only IDs)

docker ps -aq

Stop all running containers

docker stop $(docker ps -aq)

Remove all containers

docker rm $(docker ps -aq)

Remove all images

docker rmi $(docker images -q)

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.


Remove all containers created from a certain image:

docker rm  $(docker ps -a | awk '/myimage:mytag/{print $1}')

Removing all containers from Windows shell:

FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i

I found the below command is very handy to stop and remove all the containers. You don't need to stop explicitly with another command if you are using the -f (force) flag as it stops all running containers:

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

So, personally I recommend doing this as part of your deploy script for both images and containers, keeping only the most recent n containers and images. I tag my Docker images with the same versioning schema I use with git tag as well as always tagging the latest Docker image with "latest." This means that without cleaning up anything, my Docker images wind up looking like:

REPOSITORY              TAG       IMAGE ID        CREATED         VIRTUAL SIZE
some_repo/some_image    0.0.5     8f1a7c7ba93c    23 hours ago    925.4 MB
some_repo/some_image    latest    8f1a7c7ba93c    23 hours ago    925.4 MB
some_repo/some_image    0.0.4     0beabfa514ea    45 hours ago    925.4 MB
some_repo/some_image    0.0.3     54302cd10bf2    6 days ago      978.5 MB
some_repo/some_image    0.0.2     0078b30f3d9a    7 days ago      978.5 MB
some_repo/some_image    0.0.1     sdfgdf0f3d9a    8 days ago      938.5 MB

Now, of course I don't want to keep all my images (or containers) going back to perpetuity on all my production boxes. I just want the last 3 or 4 for rollbacks and to get rid of everything else. Unix's tail is your best friend here. Since docker images and docker ps both order by date, we can just use tail to select all but the top three and remove them:

docker rmi $(docker images -q | tail -n +4)

Run that along with your deploy scripts (or locally) to always keep just enough images to comfortably roll back without taking up too much room or cluttering stuff up with old images.

Personally, I only keep one container on my production box at any time, but you can do the same sort of thing with containers if you want more:

docker rm $(docker ps -aq | tail -n +4)

Finally, in my simplified example we're only dealing with one repository at a time, but if you had more, you can just get a bit more sophisticated with the same idea. Say I just want to keep the last three images from some_repo/some_image. I can just mix in grep and awk and be on my way:

docker rmi $(docker images -a | grep 'some_repo/some_image' | awk '{print $3}' | tail -n +4)

Again, the same idea applies to containers, but you get it by this point so I'll stop giving examples.


Try this command to clean containers and dangling images.

docker system prune -a

This process contains two steps (stop and remove):

  1. Stop the container that is being used by any running microservices

    docker stop $(docker ps -a -q)
    
  2. Remove all the containers

    docker rm $(docker ps -a -q)
    
  3. Remove single container

    docker rm container-ID
    

Removing older Docker containers is very easy.

List all the containers:

docker ps -a
docker ps (To list the running containers)

Once you hit docker ps -a, it will give you list of containers along with the container id (which is unique and combination of a-z, A-Z and 0-9). Copy the container id you wanted to remove and simply hit the below.

docker rm container_id

Your container will be removed along with the layers created in /var/lib/docker/aufs (if you are using Ubuntu).


Update: As of Docker version 1.13 (released January 2017), you can issue the following command to clean up stopped containers, unused volumes, dangling images and unused networks:

docker system prune

If you want to insure that you're only deleting containers which have an exited status, use this:

docker ps -aq -f status=exited | xargs docker rm

Similarly, if you're cleaning up docker stuff, you can get rid of untagged, unnamed images in this way:

docker images -q --no-trunc -f dangling=true | xargs docker rmi

I use variations of the following:

docker ps -a | grep 'cassandra.*Exited' | cut -d " " -f 1

The first part lists all processes.

The second selects just those that have 'cassandra' followed by 'Exited'.

The third, removes all the tests after the image ID, so you get a list of image ids.

So,

docker rm $(docker ps -a | grep 'cassandra.*Exited' | cut -d " " -f 1)

I am suggesting you to stop the images first and then remove.

You could go like:

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

Here is a script to remove both running and exited containers created longer than 2 days:

#!/bin/bash
# This script will kill and remove containers older than 2 days.
#
docker ps -aq > /scripts/containers.txt
today=`date +%Y-%m-%d`
oldate=`date --date="2 day ago" +%Y-%m-%d`
while read p; do
    cont=`docker inspect -f '{{ .Created }}' $p | cut -c 1-10`
    echo " Created date of $p is $cont"
    k=`echo $(( ( $(date -ud $today +'%s') - $(date -ud $cont +'%s'))/60/60/24 ))`
    echo $k
    if [ $k -ge 2 ];
    then
            echo "Killing docker container $p"
            docker kill $p
            echo "Removing docker container $p"
            docker rm $p
    else
            echo "Docker container $p is not one day old, so keeping the container."
    fi
done </scripts/containers.txt

From my experience, you should stop containers before removing them to avoid things like "this container is still running" kind of errors, so:

sudo /usr/bin/docker ps -aq |  awk '{print $1}' | \
xargs --no-run-if-empty bash -c 'sudo docker stop $@; sudo docker rm $@' bash

I keep an alias in my dotfiles like:

alias wipedocker="sudo /usr/bin/docker ps -aq |  awk '{print $1}' \
| xargs --no-run-if-empty bash -c 'sudo docker stop $@; sudo docker rm $@' bash"

If you do not like to remove all containers, you can select all containers created before or after a specific container with docker ps -f before=<container-ID> or with docker ps -f since=<container-ID>

Let's say you have developed your system, and now it is working, but there are a number of containers left. You want to remove containers created before that working version. Determine the ID of the working container with docker ps.

Remove containers created before an other container

docker rm $(docker ps -f before=9c49c11c8d21 -q)

Another example. You have your database already running on a Docker container. You have developed your application to run on another container and now you have a number of unneeded containers.

Remove containers created after a certain container

docker rm $(docker ps -f since=a6ca4661ec7f -q)

Docker stores containers in /var/lib/docker/containers in Ubuntu. I think extra containers do no other harm, but take up disk space.


You can remove the containers using multiple ways that I will explain them in the rest of the answer.

  1. docker container prune. This command removes the all of the containers that are not working right now. You can find out which containers are not working by comparing the output of docker ps and docker ps -a. The containers that are listed in docker ps -a and not exist in docker ps are not working right now, but their containers aren't removed.

  2. docker kill $(docker ps -aq). What this command does is that by executing $(docker ps -aq) it returns the list of ids of all containers and kill them. Sometime this command doesn't work because it is being using by the running container. To make that work, you can use --force option.

  3. docker rm $(docker ps -aq). It has the same definition as the second command. The only difference of them is that it removes the container (as same as docker prune), while the docker kill doesn't.

  4. Sometimes it is needed to remove the image, because you have changed the configuration of the Dockerfile and need to remove it to rebuild it. For this purpose you can see all of the images by running docker images and then copy the ID of the image that you want to remove. It can be deleted simply by executing docker image rm <image-id>.

PS: You can use docker ps -a -q instead of docker ps -aq and there is no differences. Because in unix-based operating system, you can join the options like the above example.


Here is my docker-cleanup script, which removes untagged containers and images. Please check the source for any updates.

#!/bin/sh
# Cleanup docker files: untagged containers and images.
#
# Use `docker-cleanup -n` for a dry run to see what would be deleted.

untagged_containers() {
  # Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
  docker ps -a | awk '$2 ~ "[0-9a-f]{12}" {print $'$1'}'
}

untagged_images() {
  # Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
  # NOTE: intermediate images (via -a) seem to only cause
  # "Error: Conflict, foobarid wasn't deleted" messages.
  # Might be useful sometimes when Docker messed things up?!
  # docker images -a | awk '$1 == "<none>" {print $'$1'}'
  docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
}

# Dry-run.
if [ "$1" = "-n" ]; then
  echo "=== Containers with uncommitted images: ==="
  untagged_containers 0
  echo

  echo "=== Uncommitted images: ==="
  untagged_images 0

  exit
fi

# Remove containers with untagged images.
echo "Removing containers:" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true

# Remove untagged images
echo "Removing images:" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi

Source: https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup


To remove ALL containers:

sudo docker ps -a | grep -v CONTAINER | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm -f

Explanation:

sudo docker ps -a

Returns a list of containers.

awk '{print $1}'

Gets the first column which is the container ID.

grep -v CONTAINER

Remove the title.

The last pipe sends the IDs to sudo docker rm -f safely.


You can use the following command to remove the exited containers:

docker rm $(sudo docker ps -a | grep Exit | cut -d ' ' -f 1)

Here is the full gist to also remove the old images on docker: Gist to remove old Docker containers and images.


#!/bin/bash
# docker-gc --- Remove stopped docker containers

RUNNING=$(docker ps -q)
ALL=$(docker ps -a -q)

for container in $ALL ; do
    [[ "$RUNNING" =~ "$container" ]] && continue
    echo Removing container: $(docker rm $container)
done

These two lines of Bash will filter containers by some keywords before deleting them:

containers_to_keep=$(docker ps -a | grep 'keep\|Up\|registry:latest\|nexus' | awk '{ print $1 }')
containers_to_delete=$(docker ps -a | grep Exited | grep -Fv "$containers_to_keep" | awk '{ print $1 }')
docker rm $containers_to_delete

From this post.


Use:

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

It forcefully stops and removes all containers present locally.


If you want to remove all containers then use

docker container prune

This command will remove all containers

Why not clean whole docker system with

docker system prune


Remove all stopped containers:

docker rm $(docker ps -a | grep Exited | awk '{print $1}')

From the comment by pauk960:

Since version 1.3.0 you can use filters with docker ps, instead of grep Exited use docker ps -a -f status=exited. And if you use -q with it you can get container IDs only instead of full output, no need to use awk for that.


You can stop the docker container and once it is stopped you can remove the container.

Stop the container:

$ docker stop "containerID" - you can also mention the first two letters of the container ID, and it works.

Remove the container

$ docker rm "containerID" - again you can also mention the first two letters of the container

If these command do not let you stop/remove the containers, m,ake sure you have sudo access to docker host.


https://github.com/HardySimpson/docker-cleanup

Docker cleanup

A tiny all-in-one shell, which removes:

  • Containers that not running more than one day ago
  • Images that don't belong to any remaining container

Intend to run as a crontab job

Feature

  • It will remove all <none>:<none> images
  • If the image has multiple repo:tag references to it, it will remove all repo:tag except with running a container. Actually it is a nature of "docker rmi".
  • Many error message will be show on screen, and you can decide to 2>/dev/null or not
  • Learn something from docker-gc, and fix its problem (it can not remove image that has mutliple repo:tag)

On Ubuntu 14.04 (Trusty Tahr):

$ for CON in `docker ps -qa`; do docker rm $CON ; done

This is just a normal Bash command so it should work with EVERY Bash-compliant terminal.


For Windows you can run this to stop and delete all containers:

@echo off
FOR /f "tokens=*" %%i IN ('docker ps -q') DO docker stop %%i
FOR /f "tokens=*" %%i IN ('docker ps -aq') DO docker rm %%i
FOR /f "tokens=*" %%i IN ('docker images --format "{{.ID}}"') DO docker rmi %%i

You can save it as a .bat file and can run to do that:

enter image description here


First, stop running containers before attempting to remove them

Remove running containers

docker rm $(docker stop -t=1 $(docker ps -q))

You could use kill instead of stop. In my case I prefer stop since I tend to rerun them vs. creating a new one every time so I try to shut them down nicely.

Note: Trying to stop a container will give you an error:

Error: Impossible to remove a running container, please stop it first

Remove all containers

docker rm $(docker ps -a -q)

On windows (powershell) you could do something like this:

  1. $containers = docker ps -a -q
  2. foreach ($container in $containers) {docker rm $container -f }

I wanted to add this simple answer as I didn't see it, and the question is specifically "old" not "all".

sudo docker container prune --filter "until=24h"

Adjust the 24h for whatever time span you want to remove containers that are older than.