Skip to main content

Clean up old Docker images from local storage with retention controls

Project description

Clean Up Images from Self-Hosted Docker Registries

If you're running a self-hosted Docker registry (using tools like unregistry), you'll want to clean up old images to reclaim disk space. This tool connects to your local Docker daemon and removes old images from specified repositories while keeping the ones you actually need.

The cleanup logic is simple: keep your N most recent images and anything built in the last M days. Everything else gets removed. Images currently in use by containers are automatically skipped.

This tool is designed for managing images from self-hosted registries and probably shouldn't be used for general local Docker cleanup.

Installation

uv tool install docker-image-cleanup

Usage

The basic command takes one or more image repositories:

docker-image-cleanup myrepo/myimage

You can clean multiple repositories at once:

docker-image-cleanup myrepo/image1 myrepo/image2

Before running the cleanup for real, use --dry-run to see what would be removed:

docker-image-cleanup --dry-run myrepo/myimage

Adjust the retention policy with --num-recent and --min-age-days:

# Keep only the 3 most recent images
docker-image-cleanup --num-recent 3 myrepo/myimage

# Keep images from the last 7 days
docker-image-cleanup --min-age-days 7 myrepo/myimage

# Combine both options
docker-image-cleanup --num-recent 3 --min-age-days 7 myrepo/myimage

Features

  • Removes old Docker images based on age and recency criteria
  • Keeps a configurable number of recent images (default: 5)
  • Preserves images newer than a specified age threshold (default: 30 days)
  • Automatically skips images currently in use by containers
  • Dry-run mode to preview changes before execution
  • Human-readable disk space savings reporting
  • Structured logging for detailed operation visibility

How It Works

The cleanup process is straightforward:

  1. Lists all images for the specified repository
  2. Identifies images to keep based on retention criteria (most recent N images and images newer than M days)
  3. Removes or untags images that don't meet retention criteria
  4. Skips any images currently in use by containers
  5. Reports total disk space saved

Images with multiple tags are handled intelligently. If all tags on an image would be removed, the entire image is deleted. If only some tags would be removed, those tags are untagged but the image remains.

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docker_image_cleanup-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docker_image_cleanup-0.1.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file docker_image_cleanup-0.1.1.tar.gz.

File metadata

  • Download URL: docker_image_cleanup-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for docker_image_cleanup-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5a53f8e989ce4a5aede3cca24ab99287731b87b0d9352de7639ca9f958fb6699
MD5 555123fc51398ea4d2148a11af898f72
BLAKE2b-256 23e029b7c52df5794771ef0cb330bbea899868f642c6418efc88b3310180773d

See more details on using hashes here.

File details

Details for the file docker_image_cleanup-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: docker_image_cleanup-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for docker_image_cleanup-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7fbbf6dfde78b158ef1c31c41266d41ac4a5247d66c30cf98ce800fa3debfd01
MD5 e529198ec92732473beb268ca4be384e
BLAKE2b-256 1cde011914abd27bb7ba8b22f52df462ad5912532dd29ed0bc547562423cee24

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page