diff --git a/tests/kata-doc-to-script.sh b/tests/kata-doc-to-script.sh
index 126073fd57..7ecce4efcf 100755
--- a/tests/kata-doc-to-script.sh
+++ b/tests/kata-doc-to-script.sh
@@ -168,6 +168,7 @@ doc_to_script()
 
 	all=$(mktemp)
 	body=$(mktemp)
+	trap 'rm -f $body $all' EXIT
 
 	cat "$file" |\
 		sed -n "/^ *${bash_block_open}/,/^ *${block_close}/ p" |\
@@ -188,9 +189,6 @@ doc_to_script()
 
 	# create output file
 	[ "$check_only" = "no" ] && cp "$all" "$outfile"
-
-	# clean up
-	rm -f "$body" "$all"
 }
 
 main()