---
title: "Refresh a stale YAML node after editing in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/refresh-yaml-node-after-edit/
description: "Fix a stale YAML node in Go by looking it up again after editing its document. See why an empty Value isn’t enough to tell whether the node is valid."
version: "1.0.0"
---

# Refresh a stale YAML node after editing in Go

**oneyaml-go** is a free, open-source YAML library for Go from Recursive AI. MIT or Apache-2.0, no dependencies.

[pkg.go.dev](https://pkg.go.dev/oneyaml.dev/go) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-go) · [Docs](https://recursiveai.org/oneyaml/go/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/go/cookbook/refresh-yaml-node-after-edit/refresh-yaml-node-after-edit.zip)

After an edit, use `doc.Root().Find("title")` to read a fresh node, even if the edit changed a different key. This behavior is specific to oneyaml-go nodes; if you’re switching libraries, see the [switching guide](https://recursiveai.org/oneyaml/go/cookbook/switch-from-yaml-v3/).

```shell
go get oneyaml.dev/go
```

Requires Go 1.24+. Run in a Go module.

```go
package main

import (
	"fmt"
	"log"

	"oneyaml.dev/go"
)

func main() {
	input := "title: handbook\npages: 3\n"
	doc, err := oneyaml.Parse([]byte(input))
	if err != nil {
		log.Fatal(err)
	}
	title, err := doc.Root().Find("title")
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/pages", 4); err != nil {
		log.Fatal(err)
	}
	fmt.Println("Old node stale:", title.Stale())
	title, err = doc.Root().Find("title")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Fresh title:", title.Value())
}
```

Unzip [the example](https://recursiveai.org/oneyaml/go/cookbook/refresh-yaml-node-after-edit/refresh-yaml-node-after-edit.zip), then `go run .`.

The program reports that the old node is stale, then reads the unchanged title from a fresh node.

```text
Old node stale: true
Fresh title: handbook
```

If the edit removed or renamed the key, a fresh lookup under the old name returns `ErrNotFound` instead of reviving the old node.

## Variations

**Read the Go value without keeping a node.** Replace the second `Find` block and the final print with a `Get` into a string.

```go
var freshTitle string
if err := doc.Get("/title", &freshTitle); err != nil {
	log.Fatal(err)
}
fmt.Println("Fresh title:", freshTitle)
```

```text
Old node stale: true
Fresh title: handbook
```

## Related recipes

- [Update YAML map values while iterating](https://recursiveai.org/oneyaml/go/cookbook/update-yaml-mapping-values-in-a-loop/)
- [Locate a YAML value in the source](https://recursiveai.org/oneyaml/go/cookbook/locate-yaml-value-in-source/)

## The short answer

Start from the current document again. After `Set`, call `doc.Root().Find("title")` to get a fresh node, or call `doc.Get` with a pointer if you only need the decoded Go value. Calling `Find` on an old root node doesn’t work: the root you held before the edit is stale too.

## Why is my YAML node stale after editing a different key?

A node isn’t a permanent handle to one key. It views the document as it was when you read it. An edit invalidates earlier nodes across the document, not just the node whose text changed. That’s why updating `pages` makes the old `title` node stale even though the title text stays `handbook`. `Stale()` reports that distinction directly.

Don’t use `Value() == ""` to diagnose the problem. An empty scalar can legitimately have that value, and a stale node also returns an empty string. Use `Node.Err()` if you need to distinguish a readable node, an `ErrStaleNode`, and an `ErrNotFound` from a zero node. Methods such as `Decode` and `Pointer` report staleness as an error instead of returning a usable decoded value or address. `Find` also distinguishes a stale receiver from a missing key; `Lookup` reports `false` in either case. See [Reading while you edit](https://recursiveai.org/oneyaml/go/docs/editing/#reading-while-you-edit) for the exact return behavior.

## How do you read a YAML value again after an edit?

Get a new root and descend from it, as the program does. For a nested node, repeat the lookup from that new root or use the node’s path. If you need the path from the original node, call `Pointer` **before** editing. A pointer is an address, not an identity: a rename may change the path, and inserting or removing a sequence item can change which value an index refers to. A saved pointer is only useful if it still points where you intend.

When you want the old data for comparison, decode or copy it before the edit and keep the Go value. When you want the current data, read again after the edit. Neither approach needs to keep a node alive across writes. Choose a fresh node when you also need information from the YAML source, such as its comment or position.
