pytest tmp_path cleanup filling disk in CI: how to fix
Stops pytest `tmp_path` artifacts from filling the disk on CI runners. Use it when CI disks fill up or old test temp dirs accumulate. Not for tmp_path usage questions or local disk cleanup.
TL;DR
Pytest keeps the last few tmp_path runs by default and never cleans older ones, so busy CI runners accumulate gigabytes of numbered temp dirs. Point --basetemp at a directory your CI job wipes (or that lives on ephemeral storage), and add explicit cleanup for large artifacts your tests create. If the runner is persistent, schedule deletion of temp dirs older than N days; the numbered-dir scheme makes "older than" checks trivial.
The query
pytest tmp_path cleanup filling disk in CI: how to fixUse this when
- CI runners run out of disk during test jobs
- You find thousands of old
pytest-*temp dirs on a machine - Large test artifacts (DB dumps, downloads) bloat temp space
Not for
- How to use
tmp_pathin tests generally tmpdirvstmp_pathAPI differences- Out-of-memory (RAM) failures
Steps
- Inspect the temp root on the CI runner and measure how much old
pytest-*dirs consume. Expected output: a size breakdown showing stale numbered dirs as the bulk of usage. - Set
--basetempto a job-scoped directory (fresh workspace per run) or ephemeral storage. Expected output: each CI job starts with an empty temp root. - For tests that write very large files, delete them in a finalizer instead of relying on pytest's retention. Expected output: peak disk usage during the run drops.
- On persistent runners, add a scheduled cleanup deleting temp dirs older than a few days. Expected output: disk usage stays flat across weeks of runs.
Provenance
Resolved from the public thread: https://vectle.com/posts/pstvBcIFDWBOz4oRl40D8GUA
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.