Skip to content

The Cleanup Pass That Deleted 223 Proof Screenshots

Adityo Guni Waluyo

A routine cleanup script removed 223 step-proofs in one pass because paths were compared raw: './x' vs 'x'. The fix: normalize, then regenerate one full run.

TL;DR

A cleanup script compared raw path strings without normalizing, so every screenshot looked stale and 223 artifact files were deleted right after a green test batch. Wrapping both sides in PurePath fixed the mismatch. Re-running the full suite regenerated a coherent set of artifacts more reliably than patching individual files.

The deep-proof batch for 11 admin modules had just finished. 33 green tests, about 30 minutes, a full report with a screenshot for every verification step. I ran the routine cleanup script that drops screenshots no longer referenced by the manifest. Its output was a single line: 223 files removed.

That line turned out to be nearly the entire artifact directory.

The next find command returned the manifest, the videos, and zero PNG files. Every step-proof across every manual group was gone in a single pass. The image links in the report had become dead images, minutes after the batch was reported fully green.

The wrong guess: manifest and group filter

My first guess was wrong. I suspected the group filter on the manifest was reading things incorrectly, so that proof entries were treated as not belonging to this batch. I opened the manifest and matched several filenames one by one: the manifest recorded login.png, form-submit.png, and hundreds of others correctly. Nothing was missing on the bookkeeping side.

What was missing were the files themselves, and what removed them was the very script I wrote to keep the artifacts clean.

Reading the cleanup logic again, the mistake looked trivial. The script built the list of files to keep from the manifest, then compared it against the directory contents. The comparison ran on raw path strings. The manifest wrote names with a ./ prefix, while the directory iteration produced names without that prefix. Two strings for the same file never compared equal.

As a result, the "still referenced" test failed open. Every file looked stale, and every file was removed with it.

Normalize the path before comparing

The fix is to make the path shape uniform before comparing. Python offers two routes for this. The os.path.normpath function documents itself as an operation that "collapsing redundant separators and up-level references": A//B, A/B/, A/./B, and A/foo/../B all become A/B [1]. The pathlib module takes a more careful position. PurePath "spurious slashes and single dots are collapsed, but double dots (..) and leading double slashes (//) are not", because changing them can change the meaning of a path [2]. In other words, PurePath('foo/./bar') shrinks to foo/bar, while PurePath('foo/../bar') is deliberately left alone.

# broken version
keep = {entry["screenshot"] for entry in manifest}
for png in Path("artifacts/e2e").glob("*.png"):
    if str(png) not in keep:   # "./x" vs "x" -> never equal
        png.unlink()
from pathlib import PurePath

keep = {str(PurePath(entry["screenshot"])) for entry in manifest}
for png in Path("artifacts/e2e").glob("*.png"):
    if str(PurePath(png)) not in keep:   # "./x" and "x" -> the same now
        png.unlink()

Re-run the script in dry mode first and inspect its output. The healthy pattern: zero files removed, or only genuinely stale files appear in the list. If the list is long again, normalization has not been applied to both sides of the comparison.

Two scope notes. First, string-based normalization can change the meaning of paths in trees that involve symlinks [1][2], so this practice applies to flat artifact name lists like screenshots and manifests, not to symbolic directories. Second, the same structural trap still lurks: a destructive script should fail closed. Remove only files that match a staleness pattern positively, not files that fail to match a keep pattern.

Recover with one whole run

For recovery, I chose to re-run all four specs in full instead of patching files one by one. The reason lies in Playwright's own behavior. TestConfig.outputDir is documented as "the output directory for files created during test execution", and that directory "is cleaned at the start" of every run, with a unique subdirectory per run so parallel tests do not collide [3]. Manifest, PNGs, and videos are per-run artifacts that are meant to be regenerated every time.

The full re-run took about 30 minutes and produced 33 green tests, with manifest, screenshots, and videos all coming from one coherent run. I found no evidence of a backup at the time of the loss; the recovery log only shows the re-run path, so the claim stops there.

One small defect surfaced in the same batch. A form screenshot filename was written without its repo-relative prefix, and that produced the only broken <img> in the report. The fix went into the spec and the manifest, then was re-verified in the same run.

The lesson that survives this incident is not about the admin modules under test; it is about the tool that holds bulk removal rights. A cleanup script that compares raw paths can remove 223 proofs in a single pass without a single error appearing in the log. It has to be written more carefully than the code it cleans, and once artifacts are already lost, regenerating them with one whole run is always more consistent than restoring a few files by hand.

Sources

  • [1] os.path — Common pathname manipulations, Python 3 documentation docs.python.org
  • [2] pathlib — Object-oriented filesystem paths, Python 3 documentation docs.python.org
  • [3] TestConfig — Playwright API reference playwright.dev

Related articles

A Raw Path Comparison Deleted 223 Proof Files · Adityo GW