Tag Archives: docker commands

How to Delete Unused Docker Images Safely

To manage Docker images, first check existing images. Use “docker image prune” to remove dangling images and “docker image prune -a” for all unused images. You can also filter images by age. Additionally, script removal for images older than six months or one year to keep the workspace clean.

Step 1: Check all images present

docker images

Step 2: Delete unused (dangling) images

These are <none> images left behind after builds:

docker image prune

To remove all unused images (not referenced by any container):

docker image prune -a

⚠️ This will only delete images not tied to any container.

Common options:

  • -a or --all → Remove all unused images, not just dangling ones.
docker image prune -a

-f or --force → Skip confirmation prompt.

docker image prune -f

--filter → Apply conditions (e.g., age).

docker image prune --filter "until=240h"

Note: until=240h means remove images older than 240 hours (~10 days).

You can use h (hours), m (minutes), d (days).

Examples:

-- Remove all unused images:
docker image prune -a

-- Remove images older than 6 months (~4320 hours):
docker image prune --filter "until=4320h"

-- Remove all images older than 1 year (~8760 hours):
docker image prune -a --filter "until=8760h"

Step 3: Delete images older than 6 months or 1 year

Docker doesn’t have a built-in “age filter,” but you can script it.

Example: Delete images older than 6 months

docker images --format "{{.ID}} {{.CreatedSince}}" | grep "months" | awk '{print $1}' | xargs docker rmi