evaluates expr once in each Wolfram Language session, always returning the result from the first evaluation.


evaluates expr once and caches the result in persistence location loc.

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:
  • ExpirationDateNonewhen the cache should expire
    PersistenceTimeNonehow 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.


open allclose all

Basic Examples  (1)

Evaluate an expression inside Once:

Any subsequent evaluation of Once[expr] simply returns the previous result:

Scope  (2)

Evaluate an expression only once and remember the result persistently in a local file:

Evaluate an expression only once per front end session:

The value is remembered even after a kernel restart:

Options  (2)

PersistenceTime  (1)

Specify that the result of Once should be remembered only for 5 seconds:

After the time specified has elapsed, the value is recomputed:

ExpirationDate  (1)

Specify that a remembered value should expire at the beginning of next year:

Applications  (2)

Ensure that an initialization file is read only once:

Perform a time-consuming computation only once, even if the cell containing it is evaluated again:

Properties & Relations  (3)

Once[Get[package]] is similar to Needs[package] and causes a package to be read only once:

Once has attribute HoldFirst and compares unevaluated expressions:

Find all persistently cached results of evaluations of Once:

Delete them:

Possible Issues  (2)

Once[expr] is never evaluated again, even if variables in expr may have changed:

Use Unset[Once[expr]] to clear a remembered value:

Neat Examples  (1)

Use Once for a form of dynamic programming to turn recursion into iteration:

Find the number of cached values generated:

Remove the cached values before redoing the timing experiment:

Wolfram Research (2016), Once, Wolfram Language function, https://reference.wolfram.com/language/ref/Once.html (updated 2019).


Wolfram Research (2016), Once, Wolfram Language function, https://reference.wolfram.com/language/ref/Once.html (updated 2019).


@misc{reference.wolfram_2020_once, author="Wolfram Research", title="{Once}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/Once.html}", note=[Accessed: 03-March-2021 ]}


@online{reference.wolfram_2020_once, organization={Wolfram Research}, title={Once}, year={2019}, url={https://reference.wolfram.com/language/ref/Once.html}, note=[Accessed: 03-March-2021 ]}


Wolfram Language. 2016. "Once." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/Once.html.


Wolfram Language. (2016). Once. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Once.html