Skip to main content
Docker

A Docker Clean Up Cheat Sheet

Need to quickly clean up your Docker environment? Here's a simple list of Linux and PowerShell commands to remove old images, volumes, and containers. Just copy, run, and you're good to go πŸš€

β€” Christian Schou

Are you looking for an easy way to clean up your Docker environment? I recently came across several threads on another website where users were asking how to clean up their Docker servers.

This included everything from images and volumes to containers with volumes, or even the entire environment. It's fairly easy, but since it's not something we do every day, most people would need to look it up each time.

Below is a quick command to help you clean up different components. Just copy the command and run it on your system. Bon appétit! 🀌

Linux 🐧

The commands you will find below is for Linux only. If you are looking for Windows commands (PowerShell) please go here. Windows Commands.

Delete All Containers Including Their Volume 🚧

To delete all containers and their volumes on your system, you can use the below command.

docker rm -vf $(docker ps -aq)

Delete All Images πŸ“¦

To delete all images on your system, you can use the command below.

docker rmi -f $(docker images -aq)

Delete Containers In Exited State 🏚️

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

Delete Containers In Created State 🏠

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

Delete Everything ☠️

πŸ€“
If you are looking for a way to prune your whole system, you can use the command below. Please be aware that it will remove EVERYTHING NOT IN USE! It's a great way to free up diskspace if you are running low.
docker system prune -a --volumes

Running it will result in the following output in your terminal.

WARNING! This will remove:
    - all stopped containers
    - all networks not used by at least one container
    - all volumes not used by at least one container
    - all images without at least one container associated to them
    - all build cache

Windows (PowerShell) πŸͺŸ

PowerShell treats $() as a subexpression operator, just like in the shell, but it needs to be used differently in some cases. Due to that, we can try using ForEach-Object for more robustness when running the command.

Delete All Containers Including Their Volume 🚧

To delete all containers and their volumes on your system, you can use the below PowerShell command.

docker ps -aq | ForEach-Object { docker rm -vf $_ }

Delete All Images πŸ“¦

To delete all images on your system, you can use the PowerShell command below.

docker images -aq | ForEach-Object { docker rmi -f $_ }

Delete Containers In Exited State 🏚️

Looking for a way to delete all the containers in an exited state? Remember to manually remove the volumes after, if any is attached.

docker ps -a -f status=exited -q | ForEach-Object { docker rm $_ }

Delete Containers In Created State 🏠

Looking for a way to delete all the containers in an created state? Remember to manually remove the volumes after, if any is attached.

docker ps -a -f status=created -q | ForEach-Object { docker rm $_ }

Delete Everything ☠️

This command is the same across Windows and Linux. It will give you the same warning as it does on Linux.

docker system prune -a --volumes
WARNING! This will remove:
    - all stopped containers
    - all networks not used by at least one container
    - all volumes not used by at least one container
    - all images without at least one container associated to them
    - all build cache

Summary

I hope you got your problem solved and learned something at the same time.

Resources πŸ“š

Below are links for reference to the locations with documentation I have used to create this Docker clean up tutorial.

docker system prune
″”
Christian Schou