PersistentValue
represents the persistent value associated with the key "name".
represents the persistent value associated with name stored in persistence location loc.
represents the persistent value associated with name at the first of the locations loci at which it occurs.
Details and Options



- PersistentValue["name",…] evaluates to give the corresponding persistent value.
- PersistentValue["name",…]=value sets a persistent value.
- PersistentValue allows the following options:
-
MergingFunction Automatic the function applied to the list of values found ValuePreprocessingFunction Identity the function applied to new values to be assigned ExpirationDate None when the value should expire PersistenceTime None how long the value should persist - In PersistentValue["name",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"] - Supported location types include:
-
"KernelSession" current invocation of the Wolfram Language kernel "FrontEndSession" current invocation of the Wolfram Notebook front end "Notebook" the notebook in which the current evaluation is being done "ServerSession" persistence for requests on a single web server "CookieManaged" persistence based on a Wolfram browser cookie "Local" local to a computer, based on user home directory "LocalShared" local to a computer, based on $BaseDirectory "Cloud" in the cloud defined by the current $CloudBase "Installation" installation defaults - PersistentValue["name"] is equivalent to PersistentValue["name",$PersistencePath].
- PersistentValue["name"]=value is equivalent to PersistentValue["name",$PersistenceBase]=value.
- PersistentValue["name",loc] is essentially PersistentObject["name",loc]["Value"].
- When a single persistence location is specified, as in PersistentValue["name",loc,…], the setting for MergingFunction is not used.
- Otherwise, with the setting MergingFunction->f, the final value returned is the result of applying the function f to the list of values found at all persistence locations.
- Unless specified otherwise by "MergingFunction" metadata in the PersistentObject, MergingFunction->Automatic is equivalent to MergingFunction->First.
- Before a new value is assigned in PersistentValue[sym]=value, any specified value preprocessing function is applied to value before the value is assigned.
- If the preprocessing function returns $Failed, the assignment is not performed.
- If no explicit setting for ValuePreprocessingFunction is given, the setting to use is looked up on $PersistencePath.
- An expired persistent value is treated as nonexistent, and the underlying persistent object may be removed.
- Persistent values can be removed with Remove. Remove[PersistentValue["name"]] is equivalent to Remove[PersistentValue["name",$PersistenceBase]].
Examples
open allclose allBasic Examples (3)Summary of the most common use cases

https://wolfram.com/xid/0fq5yk6iujwoq-zfd0wp

Retrieve a previously saved value:

https://wolfram.com/xid/0fq5yk6iujwoq-jy2s2b

Remember a value with front end session scope:

https://wolfram.com/xid/0fq5yk6iujwoq-0oic9b
Use the saved value, even in a newly restarted kernel within the same front end session:

https://wolfram.com/xid/0fq5yk6iujwoq-kemrac
Store values for the same name at different persistence locations:

https://wolfram.com/xid/0fq5yk6iujwoq-y67lle
Retrieve the first value found:

https://wolfram.com/xid/0fq5yk6iujwoq-70vj2x

Retrieve the list of all values on $PersistencePath:

https://wolfram.com/xid/0fq5yk6iujwoq-qrlgbe

Options (8)Common values & functionality for each option
MergingFunction (3)
Create a complement of persistent values with key "lval" at various locations:

https://wolfram.com/xid/0fq5yk6iujwoq-cqxfrv
List all existing values for the key "lval" on $PersistencePath:

https://wolfram.com/xid/0fq5yk6iujwoq-bvu4r6

By default, only the first value is returned:

https://wolfram.com/xid/0fq5yk6iujwoq-7kqeo9

Join and flatten all sublists:

https://wolfram.com/xid/0fq5yk6iujwoq-r8u53h


https://wolfram.com/xid/0fq5yk6iujwoq-hpk530

Create a complement of persistent associations with name "aval" at various locations:

https://wolfram.com/xid/0fq5yk6iujwoq-otm1fh
List all existing values on $PersistencePath:

https://wolfram.com/xid/0fq5yk6iujwoq-ode0vg

Merge the values of identical keys in the associations:

https://wolfram.com/xid/0fq5yk6iujwoq-wy6rat

Define a default value for "mylist" with a custom merging function:

https://wolfram.com/xid/0fq5yk6iujwoq-0kbfum
Define additional values at different persistence locations:

https://wolfram.com/xid/0fq5yk6iujwoq-2c29n1
For looking up values, the custom merging function is now used:

https://wolfram.com/xid/0fq5yk6iujwoq-0kusfb

PersistenceTime (2)
Define a persistent value that expires in 5 seconds:

https://wolfram.com/xid/0fq5yk6iujwoq-feydku
After 5 seconds, it is no longer used:

https://wolfram.com/xid/0fq5yk6iujwoq-64fdyh


Define a persistent object that expires in 5 days:

https://wolfram.com/xid/0fq5yk6iujwoq-gd6j5n
Verify the expiration date of the underlying persistent object:

https://wolfram.com/xid/0fq5yk6iujwoq-3rai1q

ExpirationDate (2)
Define a persistent value that expires at the beginning of the year 2020:

https://wolfram.com/xid/0fq5yk6iujwoq-9fntf
Verify the expiration date of the underlying persistent object:

https://wolfram.com/xid/0fq5yk6iujwoq-5zmt4q

Define a persistent value that expires at the beginning of next year:

https://wolfram.com/xid/0fq5yk6iujwoq-j07py1
Verify the expiration date of the underlying persistent object:

https://wolfram.com/xid/0fq5yk6iujwoq-v3isv9

ValuePreprocessingFunction (1)
Define a value preprocessing function for a particular persistent value that accepts only integers:

https://wolfram.com/xid/0fq5yk6iujwoq-wy7p8l

https://wolfram.com/xid/0fq5yk6iujwoq-3vhtnm
Assign an integer at any persistence location:

https://wolfram.com/xid/0fq5yk6iujwoq-4y9vyv
Assigning anything else generates an error and the assignment is not performed:

https://wolfram.com/xid/0fq5yk6iujwoq-3p8rlt


https://wolfram.com/xid/0fq5yk6iujwoq-8bwm3f

Applications (1)Sample problems that can be solved with this function
Properties & Relations (5)Properties of the function, and connections to other functions
Persistent values are stored in persistent objects:

https://wolfram.com/xid/0fq5yk6iujwoq-znxbp5

https://wolfram.com/xid/0fq5yk6iujwoq-ykv2li

The default location of a new persistent object is given by $PersistenceBase:

https://wolfram.com/xid/0fq5yk6iujwoq-0rorvd

https://wolfram.com/xid/0fq5yk6iujwoq-53t88y

Create a complement of persistent values with key "v" at various locations:

https://wolfram.com/xid/0fq5yk6iujwoq-otdl6x
Persistent values are by default searched on all locations given in $PersistencePath:

https://wolfram.com/xid/0fq5yk6iujwoq-ynro40

The first value found is returned:

https://wolfram.com/xid/0fq5yk6iujwoq-ktafgq

With MergingFunction->Identity, all values found are returned in a list:

https://wolfram.com/xid/0fq5yk6iujwoq-fr72sg

Specify an explicit list of locations to search for values:

https://wolfram.com/xid/0fq5yk6iujwoq-hyzsk4

Expiration times specified with PersistenceTime are converted to absolute expiration dates:

https://wolfram.com/xid/0fq5yk6iujwoq-4wzuuj

https://wolfram.com/xid/0fq5yk6iujwoq-omrh0l

Persistent values can be removed with Remove:

https://wolfram.com/xid/0fq5yk6iujwoq-xeaxv2

https://wolfram.com/xid/0fq5yk6iujwoq-blsooy

https://wolfram.com/xid/0fq5yk6iujwoq-j8o8qt

Possible Issues (1)Common pitfalls and unexpected behavior
Create two persistent values for "name" in different locations:

https://wolfram.com/xid/0fq5yk6iujwoq-dhsrmc
Remove by default removes only the value in the location given by $PersistenceBase:

https://wolfram.com/xid/0fq5yk6iujwoq-y8e29a

https://wolfram.com/xid/0fq5yk6iujwoq-jdbo4p

Remove all values that might exist anywhere in the locations given by $PersistencePath:

https://wolfram.com/xid/0fq5yk6iujwoq-6iica

https://wolfram.com/xid/0fq5yk6iujwoq-3dvr9p

Wolfram Research (2017), PersistentValue, Wolfram Language function, https://reference.wolfram.com/language/ref/PersistentValue.html (updated 2019).
Text
Wolfram Research (2017), PersistentValue, Wolfram Language function, https://reference.wolfram.com/language/ref/PersistentValue.html (updated 2019).
Wolfram Research (2017), PersistentValue, Wolfram Language function, https://reference.wolfram.com/language/ref/PersistentValue.html (updated 2019).
CMS
Wolfram Language. 2017. "PersistentValue." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/PersistentValue.html.
Wolfram Language. 2017. "PersistentValue." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/PersistentValue.html.
APA
Wolfram Language. (2017). PersistentValue. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PersistentValue.html
Wolfram Language. (2017). PersistentValue. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PersistentValue.html
BibTeX
@misc{reference.wolfram_2025_persistentvalue, author="Wolfram Research", title="{PersistentValue}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/PersistentValue.html}", note=[Accessed: 27-April-2025
]}
BibLaTeX
@online{reference.wolfram_2025_persistentvalue, organization={Wolfram Research}, title={PersistentValue}, year={2019}, url={https://reference.wolfram.com/language/ref/PersistentValue.html}, note=[Accessed: 27-April-2025
]}