Ir al contenido
Featured

Docker: Mass Cleanup Command with Exceptions

Procedure and advanced console command to remove Docker images while preserving key components of critical services.

Technical difficulty:
0views

When managing database servers or services integrated with Docker, local storage can quickly saturate with orphaned or obsolete images. To perform a mass cleanup while preserving images associated with a specific service (for example, Plane), we use the following Bash pipeline:

Ventana de terminal
docker images | grep -v "plane" | grep -v "REPOSITORY" | awk '{print $3}' | xargs -r docker rmi

⚠️ Important Warning regarding Dependencies

Sección titulada «⚠️ Advertencia Importante sobre Dependencias»

Analyzing the structure of the Plane service suite, the following support base images are identified in the local engine: postgres, rabbitmq, minio, and valkey.

  • Service Coupling: It is very likely that Plane requires and uses these images for its correct operational functioning (database storage, message queues, local S3, and caching).
  • Docker Behavior: If you delete these base images, the next time you attempt to start Plane, Docker will be forced to download them from the public registry. Ensure you have a stable internet connection when you restart the containers.
  • Active Security: If Plane containers are currently running on the server, the Docker daemon will block the deletion of associated images by default (preventing accidental service interruptions).

The command consists of 5 console tools optimally concatenated:

  1. docker images: Lists all images installed locally on the host, showing columns such as repository, tag, and image ID.
  2. grep -v "plane": Filters and excludes (thanks to the -v parameter / invert match) any line containing the exclusion word, protecting the entire service stack.
  3. grep -v "REPOSITORY": Filters and discards the first line corresponding to the Docker output table headers.
  4. awk '{print $3}': Processes the remaining output and exclusively selects the third column, which contains the unique image identifier (IMAGE ID).
  5. xargs -r docker rmi: Receives the list of IDs from the previous step and executes the image removal command (docker rmi). The -r parameter (no-run-if-empty) prevents failures or broken syntax in the terminal if there are no images to delete.

  • ✓ Preliminary Audit: Run docker images manually to map the current disk state.
  • ✓ Declarative Exclusion: Ensure that the exclusion terms in grep -v cover all active production tags.
  • ✓ Execution Check: Prefer stopping the stack via Compose before performing aggressive engine cleanups.