Docker: Remove All Images and Containers

By  on  
Docker

I've moved to a new project at Mozilla which uses a much different stack than I'm used to; suddenly I'm thrust into a world of Mercurial, Docker, and a few other technologies I'm not accustomed to.  You know what that leads to:  foul language, frustration, booze, and...lots of starting over.  Some hate starting over but wiping the slate clean provides me a bit of ease, knowing that I'm not compounding the problem by trying to patch a fix of a shim.

Starting over in this case means wiping clean my Docker images and containers, just to make sure there are no conflicts or duplicates.  The following commands delete all containers and images:

# Delete every Docker containers
# Must be run first because images are attached to containers
docker rm -f $(docker ps -a -q)

# Delete every Docker image
docker rmi -f $(docker images -q)

Of course you don't want to do this if you're using Docker across multiple projects -- you'll find yourself in a world of hurt if you break your other images and containers.

There you have it -- a clean Docker slate in one pass!

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

Incredible Demos

  • By
    Dynamically Load Stylesheets Using MooTools 1.2

    Theming has become a big part of the Web 2.0 revolution. Luckily, so too has a higher regard for semantics and CSS standards. If you build your pages using good XHTML code, changing a CSS file can make your website look completely different.

  • By
    The Simple Intro to SVG Animation

    This article serves as a first step toward mastering SVG element animation. Included within are links to key resources for diving deeper, so bookmark this page and refer back to it throughout your journey toward SVG mastery. An SVG element is a special type of DOM element...

Discussion

  1. Raul Leite

    -f to brute force and resolve any conflicts between images and containers:

    docker rmi -f $ (docker images -q)
    
    docker rm -f $ (ps docker -q -a)
    
  2. #Stop all dockers

    docker stop $(ps docker -q -a)
    
  3. Ivan

    Force delete everything with attached volumes:

    docker rm -vf $(docker ps -aq)
    • Vish

      This doesn’t work.

  4. There is also new docker command, that will delete all unused images and volumes. It is less brute force and is usually save enough to put in cron on dev machine.

    docker system prune --all

    https://docs.docker.com/engine/reference/commandline/system_prune/

  5. Umesh

    How to remove all docker images in Windows 10?

  6. There is also new docker command, that will delete all unused images and volumes. It is less brute force and is usually save enough to put in cron on dev machine.

    docker system prune --all
    

    https://docs.docker.com/engine/reference/commandline/system_prune/

  7. Vish

    Perhaps you should have added how to remove all volumes too.

  8. There is also new docker command, that will delete all unused images and volumes. It is less brute force and is usually save enough to put in cron on dev machine. Ok!

  9. pavnesh yadav

    try this command

    docker system prune --all

    – all stopped containers
    – all networks not used by at least one container
    – all images without at least one container associated to them
    – all build cache

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!