Azaria Gebremichael
Azaria Gebremichael

Reputation: 772

Remove multiple Docker containers based on creation time

I have many Docker containers both running and exited. I only want to keep the containers that were created before today/some specified time -- I would like to remove all containers created today. Easiest way to approach this?

Upvotes: 0

Views: 273

Answers (1)

araisch
araisch

Reputation: 1940

Out of the box on all OS there is the possibility to remove all containers younger than a given one:

docker rm -f $(docker ps -a --filter 'since=<containername>' --format "{{.ID}}")

so the container given in since will be kept, but all newer ones not. Maybe that suits your use case.

If you really need a period of time there will be some bash magic doing that. But specify your needs exactly then..

In detail:

docker rm: removing one or more containers
-f: force running containers to stop
docker ps -a: listing all containers
--filter 'since=..' filtering containers created since given with all details
--format "{{.ID}}": filtering ID-column only 

Upvotes: 2

Related Questions