Pcornel
Automation · Error Handling

Cleanup

Cheat sheet · https://pcornel.dk/topic.php?topic=5981

Back to the Topic

Answer

Cleanup removes temporary files, sessions, locks and partially created resources when a job ends or fails, using finally blocks, trap EXIT or always sections in playbooks.

Remember

  • Remove temp files, sessions, locks.
  • Run cleanup on failure too.
  • Use finally/trap/always.

First checks

  • trap cleanup EXIT — Bash
  • finally {} / always: — PowerShell/Ansible
  • Temp files, locks, sessions released — hygiene

Verify in this order

exit (success or failure) → cleanup → release

Where it usually breaks

Input/desired state → request accepted → job/task terminal status → target object read-back → effective runtime state → original workload → regression/rollback readiness.

Error Handling → Cleanup · operational reference

LayerError Handling → Cleanup operational reference
Runtime/ownershipautomation cleanup/finalization semantics · the automation runtime finalization path that releases temporary resources regardless of success, handled failure or cancellation
Persistent intentexplicit cleanup contract listing owned temporary files/locks/sessions/leases/transactions and which cleanup steps are safe after partial initialization
VerificationRun success, controlled-failure and cancellation fixtures that each acquire a temporary resource; inspect both primary status and cleanup logs, then query that no owned resource/lock remains.
Expected interpretationEvery path releases only resources owned by the run, primary errors remain visible, cleanup is safe to retry, and independent post-run checks show no leaked lock/session/temp object.
Recovery boundarypreserve primary and cleanup errors separately, determine resource ownership from correlation IDs, repair only leaked owned resources, then rerun failure/cancellation fixtures to prove finalization

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5981

Confirmation

Confirm action

Continue?