find -delete: Remove Matched Files in CI
find -delete removes each matching file or empty directory as it is found.
The built-in delete action is concise and avoids the quoting traps of piping to rm, but its placement and ordering rules matter.
What it does
find -delete deletes each file the expression matches. For directories it only removes empty ones, and it implies -depth so children are processed before parents. It is the safe, in-process alternative to -exec rm or xargs rm.
Common usage
find . -name '*.tmp' -delete
find build -type f -name '*.o' -delete
find /cache -type f -mtime +7 -deleteOptions
| Element | What it does |
|---|---|
| -delete | Delete matched files / empty directories |
| -type f | Restrict deletion to regular files (recommended) |
| -depth | Process directory contents before the directory (implied) |
| -mtime +N | Combine to delete only old files |
In CI
Always test with -print before swapping in -delete, since -delete acts immediately and cannot be undone. Put -delete last in the expression, after the filters; placing it before a test deletes more than you intended because find evaluates left to right.
Common errors in CI
"find: cannot delete 'X': Directory not empty" means a non-empty directory matched; restrict with -type f or remove children first. -delete is a GNU extension but BSD/macOS find supports it too. If you see "find: -delete: relative path potentially not safe", run from a fixed directory or use absolute paths.