Docker volume cleanup: inspect before deleting
Lab prerequisites
Use only an approved disposable, offline Linux VM with a local Docker Engine, ordinary local volume driver, and reviewed preloaded busybox:1.37 (record its digest, platform, exact Engine version and required utilities first). Verify the active Docker context and endpoint; every bm-v06 resource name in the lesson must be unused before starting. Never reuse a pre-existing volume. Use a dedicated lab shell, run commands individually in order, and stop on unexpected errors (the explicitly labeled negative tests are exceptions). Only delete resources you created after confirming their identity. All outputs are expected, not observed; these are manual labs, not ByteMind runnable editors. Before contacting the Engine, verify the effective local endpoint including DOCKER_HOST, DOCKER_CONTEXT and CLI overrides. Recheck the approved local image ID and native platform; if the recorded digest is unavailable, report it as unknown rather than inventing one. Use owner-set resource and time limits. If prerequisites cannot be confirmed, do the paper exercise instead. Check that the approved fixture has no declared image volumes and that the synthetic destinations and file names are unused. The bind-mount lesson alone uses a fresh host directory; do not extend it to other paths.
An unused volume can still be valuable
A volume may remain after an application is stopped or removed. That is often intentional: the next application instance may need the data. Inventory storage before deleting it, confirm ownership, check whether a stopped or running container references it, and verify backups for valuable data. A name alone is not proof that data belongs to you.
Inspect a known disposable object
Create a fresh labeled volume and a container object without starting it. The volume filter in docker ps -a includes created and stopped containers, which can still hold a volume reference. The output should show bm-v06-cleanup-holder. The label is a reminder of lab ownership, not authorization to delete other resources.
docker volume create --label bytemind.lab=v06-cleanup bm-v06-cleanupdocker create --pull=never --network none --name bm-v06-cleanup-holder -v bm-v06-cleanup:/data busybox:1.37 truedocker volume ls --filter label=bytemind.lab=v06-cleanupdocker volume inspect --format '{{.Name}} {{json .Labels}}' bm-v06-cleanupdocker ps -a --filter volume=bm-v06-cleanup --format '{{.Names}}'Release the reference, then remove the volume
The first removal attempt is intentionally expected to fail because the created container still references the volume. Record that failure; if it succeeds unexpectedly, stop the lab and investigate. Remove the holder, rerun the filter and confirm it returns no rows, then explicitly remove the lab volume. The final list should have no matching lab volume. Do not use force or prune commands to bypass this sequence. A removal error alone does not prove a container reference caused it. Verify the holder and volume identities and the reference-related diagnostic; if the cause differs, stop. Before deleting the volume, confirm no remaining references and that the data is disposable.
docker volume rm bm-v06-cleanupprintf 'status=%s\n' "$?"# Continue only after the expected failure has been confirmed.docker rm bm-v06-cleanup-holderdocker ps -a --filter volume=bm-v06-cleanup --format '{{.Names}}'# Continue only if the filter returns no rows.docker volume rm bm-v06-cleanupdocker volume ls --filter label=bytemind.lab=v06-cleanupManual practice
Given an unused production volume and a lab-owned volume with a stopped container reference, explain which checks are needed before deleting either. Do not actually delete production storage.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Docker volume cleanup: inspect before deleting — Knowledge check
Apply the concepts from this lesson.
Why is docker ps -a useful when checking volume references?
Which action normally comes first for a disposable volume still referenced by a created container?
An unused volume has no attached containers. Is that enough evidence to delete it?