WithCleanup
✖
WithCleanup
evaluates expr, running cleanup before returning the result, even if an abort, throw, etc. was generated during the evaluation of expr.
evaluates init before evaluating expr, blocking aborts, throws, etc. in both init and cleanup.
Details

- The following types of operations are blocked in various ways in init and cleanup in WithCleanup:
-
aborts Abort and interactive aborts constraints termination based on TimeConstrained, MemoryConstrained control transfers Throw, Confirm, Goto, Break, Continue, … - If an abort is initiated during the evaluation of init, it is not immediately acted upon, and expr is not evaluated, but cleanup is.
- If an abort is initiated during the evaluation of expr, that evaluation is aborted, but cleanup is still executed.
- If an abort is initiated during the evaluation of cleanup, the abort is postponed until the evaluation of cleanup is complete.
- Constraints generated externally through constructions such as TimeConstrained[... WithCleanup[...]... ] behave in WithCleanup like aborts.
- If a control transfer whose destination is outside WithCleanup[...] occurs within init or expr, the evaluation of init or expr is terminated, cleanup is evaluated, and then the transfer occurs.
- If a control transfer occurs during the evaluation of cleanup, it is not postponed.
Examples
open allclose allBasic Examples (2)Summary of the most common use cases
Clean up the assignment to x before returning the result:

https://wolfram.com/xid/0n4wyqbqq08-hf1iox

The cleanup is done even if there is an abort in the main expression being evaluated:

https://wolfram.com/xid/0n4wyqbqq08-x520i2



The same happens with other types of exceptions:

https://wolfram.com/xid/0n4wyqbqq08-8y6jnn



Scope (2)Survey of the scope of standard use cases
The cleanup is executed even though the evaluation is aborted:

https://wolfram.com/xid/0n4wyqbqq08-ua7o1i



https://wolfram.com/xid/0n4wyqbqq08-nwqbw3




The cleanup is done if an external constraint is reached during the evaluation of the main expression:

https://wolfram.com/xid/0n4wyqbqq08-gzp97l




https://wolfram.com/xid/0n4wyqbqq08-rv8etf



Constraints internal to WithCleanup are not treated specially:

https://wolfram.com/xid/0n4wyqbqq08-wais0x




Applications (1)Sample problems that can be solved with this function
Ensure that work files are properly closed even if a computation is interrupted:

https://wolfram.com/xid/0n4wyqbqq08-ehmh78

The data was safely written to disk:

https://wolfram.com/xid/0n4wyqbqq08-dsq3un
Similarly, ensure that temporary files are deleted no matter how a computation ends:

https://wolfram.com/xid/0n4wyqbqq08-1auhnn

The file was successfully deleted:

https://wolfram.com/xid/0n4wyqbqq08-5d4q1j

Properties & Relations (3)Properties of the function, and connections to other functions
An abort generated during initialization or cleanup will not prevent them from evaluating completely:

https://wolfram.com/xid/0n4wyqbqq08-11kb1o


https://wolfram.com/xid/0n4wyqbqq08-z5knkk

An abort during the evaluation of the main expression will cause its evaluation to end immediately:

https://wolfram.com/xid/0n4wyqbqq08-waskyb

An external control transfer in any argument will immediately end that argument's evaluation:

https://wolfram.com/xid/0n4wyqbqq08-8y1di6

Note that the evaluation of the cleanup will happen if the transfer starts in an earlier argument:

https://wolfram.com/xid/0n4wyqbqq08-hs9x4z

In the last argument, the control transfer takes effect immediately:

https://wolfram.com/xid/0n4wyqbqq08-sc0w1

The initialization and cleanup are effectively wrapped in AbortProtect:

https://wolfram.com/xid/0n4wyqbqq08-q5s6ll


https://wolfram.com/xid/0n4wyqbqq08-k0ggr4

Possible Issues (1)Common pitfalls and unexpected behavior
Wolfram Research (2020), WithCleanup, Wolfram Language function, https://reference.wolfram.com/language/ref/WithCleanup.html.
Text
Wolfram Research (2020), WithCleanup, Wolfram Language function, https://reference.wolfram.com/language/ref/WithCleanup.html.
Wolfram Research (2020), WithCleanup, Wolfram Language function, https://reference.wolfram.com/language/ref/WithCleanup.html.
CMS
Wolfram Language. 2020. "WithCleanup." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/WithCleanup.html.
Wolfram Language. 2020. "WithCleanup." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/WithCleanup.html.
APA
Wolfram Language. (2020). WithCleanup. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WithCleanup.html
Wolfram Language. (2020). WithCleanup. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WithCleanup.html
BibTeX
@misc{reference.wolfram_2025_withcleanup, author="Wolfram Research", title="{WithCleanup}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/WithCleanup.html}", note=[Accessed: 10-July-2025
]}
BibLaTeX
@online{reference.wolfram_2025_withcleanup, organization={Wolfram Research}, title={WithCleanup}, year={2020}, url={https://reference.wolfram.com/language/ref/WithCleanup.html}, note=[Accessed: 10-July-2025
]}