Introduced in Docker v1.25, the docker system prune command removes all:
- stopped containers;
- networks not used by at least one container;
- dangling images;
- build cache.
Additionally, you can pass some flags to the command to do the following:
- Remove All Unused Volumes;
- Remove All Unused Images;
- Remove Without Displaying Confirmation Prompt;
- Remove Containers, Images, Networks, and Volumes Based on a Filter;
Remove All Unused Volumes
By default, the docker system prune command does not prune unused volumes (to prevent loss of important data). However, you can use the --volumes option to remove volume(s) that are not used by at least one container:
docker system prune --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 dangling images # - all dangling build cache
Remove All Unused Images
You can use the --all (or -a shorthand) option, to remove all unused images (not just the dangling ones):
docker system prune -a # WARNING! This will remove: # - 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
Remove Without Displaying Confirmation Prompt
By default, the docker system prune command shows a confirmation prompt before pruning. If you don't want to see the confirmation prompt, then you can use the --force (or -f shorthand) option:
docker system prune -f
Remove Containers, Images, Networks, and Volumes Based on a Filter
Introduced in Docker v1.28, you can use the --filter option to specify which containers, images, networks, and volumes are removed. You may specify multiple filter options. Following options are supported currently:
until=<timestamp>— removes containers, images, networks, and volumes until the specified Unix timestamps, date formatted timestamps, or Go duration strings;label=...— removes containers, images, networks, and volumes with the specified labels;label!=...— removes containers, images, networks, and volumes without the specified labels;
When using the until=... filter, the local timezone on the daemon will be used unless either a Z or a +-00:00 timezone offset is specified at the end of the timestamp.
Examples:
To prune all containers, images, networks, and volumes older than 24h, you can do the following:
docker system prune --filter "until=24h" # WARNING! This will remove: # - all stopped containers # - all networks not used by at least one container # - all dangling images # - all dangling build cache # Items to be pruned will be filtered with: # - until=24h
To prune all containers, images, networks, and volumes with the label "foo", you can do the following:
docker system prune --filter "label=foo" # WARNING! This will remove: # - all stopped containers # - all networks not used by at least one container # - all dangling images # - all dangling build cache # Items to be pruned will be filtered with: # - label=foo
To containers, images, networks, and volumes that do not have the label "foo", you can do the following:
docker system prune --filter "label!=foo" # WARNING! This will remove: # - all stopped containers # - all networks not used by at least one container # - all dangling images # - all dangling build cache # Items to be pruned will be filtered with: # - label!=foo
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.