How to Batch Remove Docker Images Created Today with One (Chained) Command
2 min readOct 29, 2024
docker images --format "{{.ID}} {{.CreatedAt}} {{.Repository}}:{{.Tag}}" | grep "$(date +%Y-%m-%d)" | awk '{print $1}' | xargs docker rmi
This command finds and removes Docker images created today in a single step by chaining multiple commands together. Let’s look at each part in detail:
1. docker images --format "{{.ID}} {{.CreatedAt}} {{.Repository}}:{{.Tag}}"
This part lists all Docker images currently available on your machine.
- The
--format
flag customises the output to show: - Image ID:
{{.ID}}
- Creation Date:
{{.CreatedAt}}
- Repository Name and Tag:
{{.Repository}}:{{.Tag}}
This produces a neat list with important details about each image.
2. grep "$(date +%Y-%m-%d)"
This filters the list to show only images created today.
$(date +%Y-%m-%d)
generates today’s date in the format YYYY-MM-DD
to match the CreatedAt
field.
3. awk '{print $1}'
This extracts only the Image ID (the first column from the formatted output).
We need the Image ID for the next step, which involves deleting these images.
4. xargs docker rmi
xargs
takes the Image IDs generated byawk
and passes them to thedocker rmi
command.docker rmi
removes the specified images from your local system.
This ensures that all images created today are deleted in a batch process.
⚙️ How It Works:
- The command finds all Docker images created today.
- It extracts the relevant Image IDs.
- It removes those images by passing the IDs to
docker rmi
.
⚠️ Caution:
- Images in use: If any of the images are currently running or referenced by other containers, the command may fail for those images.
- Use with care: Make sure you don’t need the images before running the command, as this will permanently delete them.