find: Clean Build Artifacts in CI
find combines name, type, and delete predicates to sweep build artifacts out of a workspace.
Stale artifacts cause flaky builds and bloated caches. A few find one-liners keep the workspace clean between steps.
What it does
A cleanup find expression matches the artifact pattern with -name, narrows to files or directories with -type, optionally skips vendored trees with -prune, and removes matches with -delete or -exec rm. Putting filters before the action keeps deletion scoped.
Common usage
find . -type d -name __pycache__ -prune -exec rm -rf {} +
find . -type f -name '*.o' -delete
find . -type f \( -name '*.pyc' -o -name '*.coverage' \) -deleteOptions
| Pattern | What it does |
|---|---|
| -type d -name __pycache__ | Match Python cache directories |
| -prune -exec rm -rf {} + | Skip descending, then remove the dir whole |
| -name "*.o" -delete | Remove object files |
| \( -name a -o -name b \) | Match several artifact patterns at once |
In CI
To remove whole cache directories such as __pycache__, pair -prune with -exec rm -rf {} + so find does not descend into a directory it is about to delete. Run a -print dry run first; deletion is irreversible on a runner.
Common errors in CI
"find: cannot delete 'X': Directory not empty" with -delete means you tried to delete a non-empty directory; use -exec rm -rf {} + for directories or -type f for files. Removing __pycache__ without -prune can make find walk a tree it is deleting, which on some systems prints "No such file or directory" mid-walk.