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
mktemp Command Reference for CI Scriptsmktemp creates unique temp files and directories safely in CI. Reference for -d, templates, and -t, plus the…
trap Command Reference for CI Scriptstrap runs cleanup handlers on signals and exit in CI scripts. Reference for EXIT, ERR, and signal traps so te…
find Command Reference for CI Scriptsfind walks directory trees and acts on matching files in CI. Reference for -name, -type, -exec, and -print0,…
mkdir -p Command Reference for CI Scriptsmkdir -p creates directory trees idempotently in CI. Reference for -p, -m, and parent creation, plus why -p n…