Skip to content
Latchkey

rm -rf Command Reference for CI Scripts

rm -rf deletes files and directory trees without prompting.

rm -rf is the standard cleanup hammer in CI, and the most dangerous command in a script. A single unset variable can turn a targeted delete into a catastrophic one.

Common flags/usage

  • -r / -R: recurse into directories
  • -f: force, ignore missing files and never prompt
  • -d: remove empty directories
  • -v: verbose, list what is removed
  • -- : end of options, so paths starting with - are not flags

Example

shell
set -euo pipefail
BUILD_DIR="${RUNNER_TEMP}/build"
rm -rf "${BUILD_DIR:?BUILD_DIR is empty}"   # :? aborts if unset/empty
mkdir -p "${BUILD_DIR}"

In CI

The classic disaster is rm -rf "$DIR/" when $DIR is unset, which expands to rm -rf / on the runner. Guard with "${DIR:?}" so the shell aborts on an empty value, and prefer deleting a path you just created via mktemp -d. -f hides errors, so it can mask a wrong path; verify the target first.

Key takeaways

  • Guard rm -rf with "${VAR:?}" so an unset path aborts instead of deleting /.
  • Prefer removing a directory you created yourself via mktemp -d.
  • -f suppresses errors, which can hide a wrong or empty target path.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →