---
title: "Save"
language: "en"
type: "Symbol"
summary: "Save[filename, symbol] appends definitions associated with the specified symbol to a file. Save[filename,  patt] appends definitions associated with all symbols whose names match the string pattern  patt. Save[filename,  context`] appends definitions associated with all symbols in the specified context. Save[filename, {object1, object2, ...}] appends definitions associated with several objects."
keywords: 
- saving definitions of functions
- saving function definitions
- saving of definitions
- saving full definitions
- save
- save
- saveobj
canonical_url: "https://reference.wolfram.com/language/ref/Save.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Package Development"
    link: "https://reference.wolfram.com/language/guide/PackageDevelopment.en.md"
  - 
    title: "Wolfram Language Expressions in Files"
    link: "https://reference.wolfram.com/language/guide/WolframLanguageExpressionsInFiles.en.md"
  - 
    title: "Wolfram System Session History"
    link: "https://reference.wolfram.com/language/guide/WolframSystemSessionHistory.en.md"
  - 
    title: "Symbol Handling"
    link: "https://reference.wolfram.com/language/guide/SymbolHandling.en.md"
  - 
    title: "Persistent Storage"
    link: "https://reference.wolfram.com/language/guide/PersistentStorage.en.md"
  - 
    title: "Cloud Functions & Deployment"
    link: "https://reference.wolfram.com/language/guide/CloudFunctionsAndDeployment.en.md"
  - 
    title: "Files"
    link: "https://reference.wolfram.com/language/guide/Files.en.md"
  - 
    title: "Assignments"
    link: "https://reference.wolfram.com/language/guide/Assignments.en.md"
  - 
    title: "Wolfram Language File Formats"
    link: "https://reference.wolfram.com/language/guide/WolframLanguageFileFormats.en.md"
related_functions: 
  - 
    title: "PutAppend"
    link: "https://reference.wolfram.com/language/ref/PutAppend.en.md"
  - 
    title: "Get"
    link: "https://reference.wolfram.com/language/ref/Get.en.md"
  - 
    title: "DumpSave"
    link: "https://reference.wolfram.com/language/ref/DumpSave.en.md"
  - 
    title: "FullDefinition"
    link: "https://reference.wolfram.com/language/ref/FullDefinition.en.md"
  - 
    title: "SaveDefinitions"
    link: "https://reference.wolfram.com/language/ref/SaveDefinitions.en.md"
  - 
    title: "CloudSave"
    link: "https://reference.wolfram.com/language/ref/CloudSave.en.md"
  - 
    title: "IncludeDefinitions"
    link: "https://reference.wolfram.com/language/ref/IncludeDefinitions.en.md"
related_tutorials: 
  - 
    title: "Reading and Writing Wolfram System Files"
    link: "https://reference.wolfram.com/language/tutorial/FilesStreamsAndExternalOperations.en.md#14387"
---
# Save

Save["filename",symbol]
appends definitions associated with the specified symbol to a file. 
	Save["filename","patt"]
appends definitions associated with all symbols whose names match the string pattern "patt". 
	Save["filename","context`"]
appends definitions associated with all symbols in the specified context. 
	Save["filename",{Subscript[object, 1],Subscript[object, 2],\[Ellipsis]}]
appends definitions associated with several objects.

## Details and Options

* ``Save`` uses ``FullDefinition`` to include subsidiary definitions.

* ``Save`` writes out definitions in ``InputForm``.

* ``Save`` uses ``Names`` to find symbols whose names match a given string pattern.

* You can use ``Save["filename", "s"]`` to write out the definition for the value of a symbol ``s`` itself.

* ``Save`` works with local and cloud objects.

* ``Save[File["filename"], …]`` is also supported.

* The following options can be given:

|                   |           |                                             |
| ----------------- | --------- | ------------------------------------------- |
| ExcludedContexts  | Automatic | contexts excluded from recursive inclusion  |
| IncludedContexts  | All       | contexts considered for recursive inclusion |

---

## Examples (11)

### Basic Examples (2)

Create a temporary file:

```wl
In[1]:= tempfile = FileNameJoin[{$TemporaryDirectory, "saved.wl"}]

Out[1]= "/var/folders/97/q4wjkvlx53g_y7cwpbx1035w000c49/T/saved.wl"
```

Store a value in a variable:

```wl
In[2]:= a = "This must be saved";
```

Save the value of the variable in a file:

```wl
In[3]:= Save[tempfile, a]
```

Loading the file restores the value of ``a`` :

```wl
In[4]:= Clear[a]

In[5]:= Get[tempfile];

In[6]:= a

Out[6]= "This must be saved"

In[7]:= DeleteFile[tempfile]
```

---

Create multiple definitions to save:

```wl
In[1]:=
a = 5;
f[x_] := x ^ 2
```

Save the definitions to standard out to see how they would be written to a file:

```wl
In[2]:= Save["stdout", {a, f}]

During evaluation of In[3]:=
a = 5

f[x_] := x^2
```

### Scope (3)

Save a definition into a file specified by the ``File`` object:

```wl
In[1]:= tempfile = File@FileNameJoin[{$TemporaryDirectory, "saved.wl"}]

Out[1]= File["/var/folders/97/q4wjkvlx53g_y7cwpbx1035w000c49/T/saved.wl"]

In[2]:= a = "Some string";

In[3]:= Save[tempfile, a]

In[4]:= Clear[a]

In[5]:= Get[tempfile]

Out[5]= "Some string"

In[6]:= DeleteFile[tempfile]
```

---

Local objects can be used with ``Save`` to save definitions persistently:

```wl
In[1]:=
a = 5;
f[x_] := x ^ 2

In[2]:= Save[LocalObject["definitions"], {a, f}]

Out[2]= LocalObject["file:///home/home1/user/.Wolfram/Objects/definitions"]
```

---

Cloud objects can be used with ``Save`` to save definitions in the cloud:

```wl
In[1]:=
a = 5;
f[x_] := x ^ 2

In[2]:= Save[CloudObject["definitions"], {a, f}]

Out[2]=
CloudObject[
 "https://www.wolframcloud.com/obj/user-b0c28e9f-876d-4478-9d8b-9e7d18a9ea81/definitions"]
```

### Options (2)

#### ExcludedContexts (1)

By default, certain system-internal contexts are excluded from recursive inclusions:

```wl
In[1]:= x := $TimeZone

In[2]:=
WithCleanup[f = CreateFile[], 
	Save[f, x];FilePrint[f], 
	DeleteFile[f]]

x := $TimeZone
```

Use ``ExcludedContexts -> {}`` to save definitions of all non-protected symbols:

```wl
In[3]:=
WithCleanup[f = CreateFile[], 
	Save[f, x, ExcludedContexts -> {}];FilePrint[f], 
	DeleteFile[f]]

x := $TimeZone
 
$TimeZone = -5.
```

#### IncludedContexts (1)

Limit recursive inclusion of symbols to the contexts ``"c1`"`` and ``"c2`"`` only:

```wl
In[1]:=
x := {c1`bar, c1`Private`bar , c2`baz}
c1`bar := 5
c1`Private`bar := 1
c2`baz := 3

In[2]:=
WithCleanup[f = CreateFile[], 
	Save[f, x, IncludedContexts -> {"c1`", "c2`"}];FilePrint[f], 
	DeleteFile[f]]

x := {c1`bar, c1`Private`bar, c2`baz}
 
c1`bar := 5
 
c2`baz := 3
```

### Applications (1)

Save all definitions in the default context:

```wl
In[1]:=
a = 5;
f[x_] := x ^ 3

In[2]:= Save["stdout", "`*"]

During evaluation of In[3]:=
a = 5

f[x_] := x^3
```

### Properties & Relations (3)

``Save`` writes the output of ``FullDefinition`` to a file:

```wl
In[1]:= g[x_] := x ^ 3

In[2]:=
SetAttributes[f, Listable];
f[x_] := g[x ^ 2]

In[3]:= Save["stdout", f]

During evaluation of In[4]:=
Attributes[f] = {Listable}

f[x_] := g[x^2]

g[x_] := x^3
```

---

Saved definitions can be read into the Wolfram Language using ``Get`` :

```wl
In[1]:= f[x_] := x ^ 2

In[2]:= file = Close[OpenWrite[]]

Out[2]= "/tmp/m000001154821"

In[3]:= Save[file, f]
```

Print the contents of the saved file:

```wl
In[4]:= FilePrint[file]

f[x_] := x^2
```

Clear definitions in the current session:

```wl
In[5]:=
ClearAll[f];
Definition[f]

Out[5]= 
```

Get the definitions into the current session:

```wl
In[6]:= Get[file]

In[7]:= Definition[f]

Out[7]= f[x_] := x^2
```

---

By default, definitions attached to ``"System`"`` symbols are not pulled in:

```wl
In[1]:=
x := Subscript[y, 1]
Subscript[y, 1] := 17
WithCleanup[f = CreateFile[], 
	Save[f, x];FilePrint[f], 
	DeleteFile[f]]

x := Subscript[y, 1]
```

Use ``ExcludedContexts -> {}`` to pull in definitions from all contexts:

```wl
In[2]:=
WithCleanup[f = CreateFile[], 
	Save[f, x, ExcludedContexts -> {}];FilePrint[f], 
	DeleteFile[f]]

x := Subscript[y, 1]
 
Attributes[Subscript] = {NHoldRest}
 
Subscript[y, 1] := 17
```

Alternatively, attach definitions to your own symbols:

```wl
In[3]:=
Clear[Subscript]
y/:Subscript[y, 1] = 17
WithCleanup[f = CreateFile[], 
	Save[f, x];FilePrint[f], 
	DeleteFile[f]]

Out[3]= 17

x := Subscript[y, 1]
 
y /: Subscript[y, 1] = 17
```

## See Also

* [`PutAppend`](https://reference.wolfram.com/language/ref/PutAppend.en.md)
* [`Get`](https://reference.wolfram.com/language/ref/Get.en.md)
* [`DumpSave`](https://reference.wolfram.com/language/ref/DumpSave.en.md)
* [`FullDefinition`](https://reference.wolfram.com/language/ref/FullDefinition.en.md)
* [`SaveDefinitions`](https://reference.wolfram.com/language/ref/SaveDefinitions.en.md)
* [`CloudSave`](https://reference.wolfram.com/language/ref/CloudSave.en.md)
* [`IncludeDefinitions`](https://reference.wolfram.com/language/ref/IncludeDefinitions.en.md)

## Tech Notes

* [Reading and Writing Wolfram System Files](https://reference.wolfram.com/language/tutorial/FilesStreamsAndExternalOperations.en.md#14387)

## Related Guides

* [Package Development](https://reference.wolfram.com/language/guide/PackageDevelopment.en.md)
* [Wolfram Language Expressions in Files](https://reference.wolfram.com/language/guide/WolframLanguageExpressionsInFiles.en.md)
* [Wolfram System Session History](https://reference.wolfram.com/language/guide/WolframSystemSessionHistory.en.md)
* [Symbol Handling](https://reference.wolfram.com/language/guide/SymbolHandling.en.md)
* [Persistent Storage](https://reference.wolfram.com/language/guide/PersistentStorage.en.md)
* [Cloud Functions & Deployment](https://reference.wolfram.com/language/guide/CloudFunctionsAndDeployment.en.md)
* [`Files`](https://reference.wolfram.com/language/guide/Files.en.md)
* [`Assignments`](https://reference.wolfram.com/language/guide/Assignments.en.md)
* [Wolfram Language File Formats](https://reference.wolfram.com/language/guide/WolframLanguageFileFormats.en.md)

## Related Links

* [An Elementary Introduction to the Wolfram Language: Storing Things](https://www.wolfram.com/language/elementary-introduction/43-storing-things.html)

## History

* Introduced in 1988 (1.0) \| Updated in 1996 (3.0) ▪ [2016 (11.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn110.en.md) ▪ [2021 (12.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn123.en.md)