Deleting or replacing a file in a later Dockerfile instruction usually does not reclaim the bytes stored by an earlier image layer. The later layer changes the container’s merged filesystem view, but the earlier layer remains part of the image. To keep temporary files out of the final image, create and remove them in the same filesystem-changing instruction; use multi-stage builds to leave build-only tools and outputs behind.
Why a later deletion does not shrink an earlier layer
A Docker image is assembled from filesystem layers. When a later layer deletes a file that an earlier layer added, the deletion hides that file in the merged filesystem presented to the container. It does not reach back and erase the bytes recorded in the earlier layer. Docker describes this storage behavior in its storage drivers documentation.
As an Amazon Associate I earn from qualifying purchases.
Replacing a file has the same underlying issue: the earlier version remains represented in its layer, while the replacement is recorded later. The active filesystem view may show only the replacement, but the image contains the changes from both layers. That is the duplication penalty: one version is no longer visible, yet its stored bytes have not been removed.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minuteKeep temporary files and their cleanup in one instruction
If package indexes, archives, or other temporary files are created during an image build, remove them in the same filesystem-changing instruction when the goal is to avoid retaining them in the image. For example:
#1 Best Overall
RUN apt-get update
&& apt-get install -y --no-install-recommends ca-certificates
&& rm -rf /var/lib/apt/lists/*
Because the installation and cleanup happen within one RUN, the temporary package lists do not remain as additions in an earlier image layer followed by a deletion in a later one. Docker’s storage-driver guidance explicitly warns that removing cache files in another layer does not remove their contribution from the prior layer.
This is not a reason to combine every Dockerfile operation into one enormous RUN. Combine operations when intermediate files must disappear together; retain useful boundaries where they support cache reuse and make the build easier to maintain.
Rank #2
Separate image size from build-cache reuse
Docker’s build cache is a separate concern from the bytes stored in the final image. When an instruction’s inputs change, Docker may need to rebuild that layer and subsequent layers. Placing relatively stable dependency manifests before frequently edited application source can let dependency-installation layers remain reusable when only source files change.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →For example, a project may copy its dependency manifest, install dependencies, and only then copy the rest of its source. The exact files and commands depend on the project’s dependency model. Docker’s build cache documentation explains downstream invalidation, and its cache-use example demonstrates ordering package metadata before the rest of an application.
Rank #3
Good ordering can shorten rebuilds; it does not erase bytes already recorded in image layers. Treat cache reuse and final-image contents as distinct goals.
Use multi-stage builds to exclude build-only contents
A multi-stage build lets one stage compile or assemble an application while a later runtime stage starts from an appropriate base and copies in only selected outputs. This keeps compilers, development dependencies, and intermediate artifacts out of the final runtime image when they are not needed to run the application. Docker covers this approach in its building best practices.
Choose the copied artifacts deliberately: the runtime stage must still include everything the application needs to start and operate. Multi-stage builds solve a different problem from cleaning temporary files in one instruction: they separate build-time contents from the final runtime image.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
When COPY –link can help
In supported Dockerfile and BuildKit workflows, COPY --link can put copied files in a layer that is independent of preceding layers. That can make the copied layer reusable across changes to earlier layers. Consult the Dockerfile reference and verify that the builder and Dockerfile syntax used by your project support the option before relying on it.
Best Value
- Docker, Docker Swarm, Docker Compose, Programmer, Developer, Coding, Programming, Software Engineer, Code, DevOps, Deploy, Deployment, Kubernetes, Salt, Puppet, Chef, Terraform, Container, AWS, Azure, Cloud, Geek, Funny, Computer, Software, Tech, IT
- Integration, Scrum, Compile, Compilation, Science, Bug, Debug, Python, Linux, Java, Javascript, Scala, Dotnet, Kotlin
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
COPY --link is a cache and layer-reuse feature, not a cleanup mechanism. It does not remove unwanted files that were already added to another layer.
Do not confuse build layers with runtime OverlayFS copy-up
There is a separate use of “copy” that can matter after a container starts. Under the OverlayFS storage driver, the first time a container writes to a file that exists in a lower image layer, OverlayFS copies the whole file into the container’s writable upper directory; the write then applies to that copy. A small edit to a large file can therefore involve copying the entire file on that first write. Later writes use the upper copy rather than repeating the initial copy-up. See Docker’s OverlayFS storage-driver documentation.
This runtime behavior concerns write work and the container’s writable layer. It is not the reason a build-time deletion leaves bytes in an image, and it should not be generalized to storage drivers other than OverlayFS.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Choose the fix for the problem you actually have
- Unwanted temporary files in the final image: create and clean them in the same filesystem-changing instruction.
- Build tools or intermediate outputs in the runtime image: use a multi-stage build and copy only the required artifacts.
- Slow rebuilds after source edits: place stable dependency inputs before frequently changing source where the project permits, and preserve useful cache boundaries.
- A copied layer that should survive changes to earlier layers: consider
COPY --linkonly with a supporting builder and syntax. - Slow first writes to lower-layer files at runtime: assess the OverlayFS copy-up behavior separately from image-layer cleanup.
Docker’s reviewed documentation does not provide a general numeric estimate of image-size or build-time savings from avoiding layer duplication; the result depends on which files and build steps are involved.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




