Skip to content
Latchkey

How to Free Disk Space Before a Docker Build in CI

GitHub-hosted runners ship many preinstalled SDKs; removing the ones you do not need frees space for a big image build.

Large image builds can exhaust the runner disk and fail with no space left on device. Removing unused toolchains (or pruning the Docker cache) before the build reclaims several gigabytes.

Steps

  • Add a cleanup step before the build (a maintained action or manual rm).
  • Remove large unused directories such as Android SDK, .NET, and GHC.
  • Optionally run docker system prune -af to clear stale build cache.

Workflow

.github/workflows/docker.yml
steps:
  - name: Free disk space
    run: |
      sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc
      sudo docker system prune -af
      df -h /
  - uses: docker/build-push-action@v6
    with:
      push: false
      tags: app:ci

Gotchas

  • Deleting a toolchain a later step needs will break that step; only remove what this job will not use.
  • Print df -h / after cleanup so logs show how much space the build actually had.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →