Docker is an excellent tool for containerizing applications, but it can easily consume your server’s storage if you don’t perform regular cleanups. Old images, containers, and unused volumes can pile up, slowing down your system and causing disk space issues. In this blog, we’ll walk you through how to identify and delete Docker resources that are older than 30 days to free up space.
Remove Docker Containers Older Than 30 Days
Check before deletion:
If you want to see the containers that match this filter before deleting them, run:
docker ps -a --filter "until=720h"
Stopped containers take up unnecessary space. You can remove them with this simple command:
docker container prune --filter "until=720h"
Delete Docker Images Older Than 30 Days
Check before deletion:
To see which images will be deleted:
docker images --filter "until=720h"
For Delete the docker images used following command:
Unused images can consume a significant amount of disk space. Remove them with:
docker image prune -a --filter "until=720h"
What this does:
- Deletes all images that have not been used by a container in the last 30 days.
- The
-aflag ensures even untagged and dangling images are removed.
Clean Up Unused Volumes
Volumes don’t have direct support for time-based cleanup, but you can prune unused volumes with:
docker volume prune
Perform a Full Cleanup of Docker Resources
If you want to clean up everything older than 30 days in one go (images, containers, volumes, networks):
docker system prune -a --filter "until=720h"
What this does:
- Removes all unused containers, images, volumes, and networks older than 30 days.