Once
Details and Options
- When first used in a session, Once[expr] evaluates expr and returns the result.
- Subsequent uses of Once[expr] do not evaluate expr again, and return the result from when expr was first evaluated.
- Once has attribute HoldFirst.
- Once[expr] is equivalent to Once[expr,"KernelSession"].
- In Once[expr,loc], possible forms for the persistence location loc include:
-
PersistenceLocation["type"] "Local", "Cloud", "FrontEndSession", etc. locations "type" equivalent to PersistenceLocation["type"] PersistenceLocation["type","base"] location with a particular base (directory etc.) "type""base" equivalent to PersistenceLocation["type","base"] - Unset[Once[expr]] or Unset[Once[expr,loc]] removes any stored value for expr.
- Once has the following options:
-
ExpirationDate None when the cache should expire PersistenceTime None how long the cache should persist - If Once[expr,…] is evaluated after a cached result is specified to have expired, then expr will be evaluated again.
Examples
open all close allBasic Examples (1)
Scope (2)
Evaluate an expression only once and remember the result persistently in a local file:
res = Once[Import["http://www.wolfram.com/spikey/img/spikey.png"], "Local"]Evaluate an expression only once per front end session:
Once[Print["pong"];RandomReal[], "FrontEndSession"]The value is remembered even after a kernel restart:
Quit[]Once[Print["pong"];RandomReal[], "FrontEndSession"]Options (2)
ExpirationDate (1)
PersistenceTime (1)
Specify that the result of Once should be remembered only for 5 seconds:
Once[Now, PersistenceTime -> 5]Pause[1];Once[Now, PersistenceTime -> 5]After the time specified has elapsed, the value is recomputed:
Pause[4];Once[Now, PersistenceTime -> 5]Applications (2)
Ensure that an initialization file is read only once:
Once[Get["init.m"]]Perform a time-consuming computation only once, even if the cell containing it is evaluated again:
Once[result = CloudGet["https://www.wolframcloud.com/objects/ecdc9d78-f517-4682-93d8-790a3de79ee0"]]Properties & Relations (3)
Once[Get[package]] is similar to Needs[package] and causes a package to be read only once:
Once[Get["EquationTrekker`"]]Once has attribute HoldFirst and compares unevaluated expressions:
Once[Print["pong"];1 + 1]Once[Print["pong"];1 + 1]Once[Print["pang"];2]Once[Print["pang"];2]Find all persistently cached results of evaluations of Once:
PersistentObjects["Hashes/Once/*"]//ShortDeleteObject[%]Possible Issues (2)
Once[expr] is never evaluated again, even if variables in expr may have changed:
x = 0;
Once[x++]x = 5;
Once[x++]xUse Unset[Once[expr]] to clear a remembered value:
Once[Print["again"];1]Unset[Once[Print["again"];1]]Once[Print["again"];1]Neat Examples (1)
Use Once for a form of dynamic programming to turn recursion into iteration:
fib[0] = 0;fib[1] = 1;
fib[n_] := Once[fib[n - 1] + fib[n - 2], "KernelSession" -> "fib"];Timing[Block[{$RecursionLimit = ∞}, res = fib[6000]];N[res]]Find the number of cached values generated:
Length[PersistentObjects[All, "KernelSession" -> "fib"]]Remove the cached values before redoing the timing experiment:
DeleteObject[PersistentObjects[All, "KernelSession" -> "fib"]]See Also
LocalCache Needs PersistenceLocation ScheduledTask WithCleanup
Function Repository: OnceUnlessFailed
Related Guides
Text
Wolfram Research (2016), Once, Wolfram Language function, https://reference.wolfram.com/language/ref/Once.html (updated 2019).
CMS
Wolfram Language. 2016. "Once." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/Once.html.
APA
Wolfram Language. (2016). Once. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Once.html
BibTeX
@misc{reference.wolfram_2026_once, author="Wolfram Research", title="{Once}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/Once.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_once, organization={Wolfram Research}, title={Once}, year={2019}, url={https://reference.wolfram.com/language/ref/Once.html}, note=[Accessed: 13-June-2026]}