How to remove all docker containers using a single command

In this guide, we will show command line examples for removing all Docker containers from a Linux system. This will work on any Linux distribution.

Along with removing Docker containers, you’ll also learn how to remove Docker images, volumes, and networks. This is useful when you want to completely purge all traces of containers from your Docker installation and start fresh. Check out the examples below to find out how.

In this tutorial you will learn:

  • How to list, stop, and remove all Docker containers
  • How to remove Docker images, volumes, and networks

Removing all Docker containers from a Linux system

Removing all Docker containers from a Linux system

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Docker
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Removing Docker containers command line examples



  1. Start by using the following command to see a list of all your Docker containers. This will also list the container ID, in case you want to remove containers individually. Review this list carefully to make sure you actually want to remove all of these containers.
    $ docker container ls -a
    
  2. Listing all the Docker containers on our system

    Listing all the Docker containers on our system

  3. You can now use the docker container rm command to remove the containers you don’t want. You just need to specify the container ID of each one that you want to remove. Here’s an example where we move a couple from the list above:
    $ docker container rm 6700495a31b6 20e038f97de1
    

    Note that this command will only work for containers that aren’t running. Otherwise, you will encounter an error like the one below.

    Error response from daemon: You cannot remove a running container.
    
  4. To remove all stopped containers at once, you can use the following command.
    $ docker container prune
    
  5. To stop all containers so that they can all be removed, you can use the following command.
    $ docker stop $(docker ps -aq)
    


  6. After running the previous command, you can use this one to remove all Docker containers on your system.
    $ docker container rm $(docker container ls -aq)
    
  7. Stopping all our Docker containers, then removing them all with a single command

    Stopping all our Docker containers, then removing them all with a single command

  8. You can remove all Docker images from your system with the following command.
    $ docker rmi $(docker images -q)
    
  9. To remove all unused Docker volumes, execute the following command.
    $ docker volume prune
    
  10. Lastly, to remove all unused Docker networks, use this command.
    $ docker network prune
    

Closing Thoughts

In this guide, we saw various examples for removing all Docker containers from a Linux system. This allows you to start fresh very quickly, without fiddling around with removing containers individually. We also learned how to remove Docker images, volumes, and networks, allowing us to clean up our Docker installation even more thoroughly.



Comments and Discussions
Linux Forum