In good old days, we used to write scripts to cleanup stale Docker containers, images, volumes, etc. Docker Engine 1.25 provided a welcome change on this. Now, there’s a relatively straightforward way to cleanup unused resources using the following command.
sh
docker system prune
With this single command, you can remove all stopped containers, unused networks, dangling images, and build cache.
Even more importantly, prune
is also available for other resource commands.
sh
# removes all stopped containers
docker container prune # removes unused images
docker image prune # removes all unused networks
docker network prune # removes all unused local volumes
docker volume prune # removes build cache
docker builder prune
There are also a couple of useful flags available:
--all
,-a
removes all unused images and build cache (not merely dangling ones)--filter
provides filtering capabilities by label or timestamp. (e.g.,--filter "label!=latest"
,--filter "until=24h"
). You can pass multiple filters.--force
,-f
executes the command without any confirmation prompt
You can read more about these commands in system, container, image, network, volume and builder references.