---
title: "Restore a removed YAML node in Rust"
url: https://recursiveai.org/oneyaml/rust/cookbook/restore-removed-yaml-node/
description: "Undo a YAML removal in a local Rust edit preview by keeping the removed entry and restoring it at its original position with its comment."
version: "1.2.0"
---

# Restore a removed YAML node in Rust

**oneyaml** is a free, open-source YAML library for Rust from Recursive AI. MIT or Apache-2.0, no unsafe code.

[crates.io](https://crates.io/crates/oneyaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-rust) · [Docs](https://recursiveai.org/oneyaml/rust/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/rust/cookbook/restore-removed-yaml-node/restore-removed-yaml-node.zip)

Keep the removed entry and pass it to `insert_removed_at` to undo the deletion.

```shell
cargo add oneyaml
```

```rust
use oneyaml::{Document, path};

fn main() {
    let yaml = concat!(
        "port: 8080\n",
        "mode: 'local' # preview\n",
        "workers: 2\n",
    );
    let mut doc = Document::parse(yaml)
        .expect("valid YAML");
    let removed = doc.remove_path(&path!["mode"])
        .expect("remove mode");
    print!("Preview:\n{doc}");
    doc.root_mut().expect("root mapping")
        .insert_removed_at(1, removed)
        .expect("restore mode");
    print!("Restored:\n{doc}");
}
```

Unzip [the example](https://recursiveai.org/oneyaml/rust/cookbook/restore-removed-yaml-node/restore-removed-yaml-node.zip), then `cargo run`.

Requires Rust 1.85 or later. The snippet prints the preview and the restored YAML:

```text
Preview:
port: 8080
workers: 2
Restored:
port: 8080
mode: 'local' # preview
workers: 2
```

## The full program

The program restores the original text, then demonstrates appending the same entry instead. If the key already exists at the destination, reinsertion fails.

```rust
use oneyaml::{Document, path};

fn main() {
    let yaml = concat!(
        "port: 8080\n",
        "mode: 'local' # preview\n",
        "workers: 2\n",
    );
    let mut doc = Document::parse(yaml)
        .expect("valid YAML");
    let removed = doc.remove_path(&path!["mode"])
        .expect("remove mode");
    print!("Preview:\n{doc}");
    doc.root_mut().expect("root mapping")
        .insert_removed_at(1, removed)
        .expect("restore mode");
    print!("Restored:\n{doc}");

    assert_eq!(doc.to_string(), yaml);
    let removed = doc.remove_path(&path!["mode"]).expect("remove mode");
    doc.root_mut().expect("root mapping")
        .insert_removed(removed).expect("append mode");
    print!("Appended:\n{doc}");
}
```

`cargo run` prints:

```text
Preview:
port: 8080
workers: 2
Restored:
port: 8080
mode: 'local' # preview
workers: 2
Appended:
port: 8080
workers: 2
mode: 'local' # preview
```

## Related recipes

- [Remove a YAML key and keep its leading comments](https://recursiveai.org/oneyaml/rust/cookbook/remove-yaml-key-keep-leading-comments/)
- [Insert an item into a YAML list](https://recursiveai.org/oneyaml/rust/cookbook/insert-yaml-sequence-item/)

## The short answer

Keep the `Removed` returned by `remove_path`. While working on the same editable `Document`, call `insert_removed_at` on the original parent and pass the old index and the saved `Removed`. In the example, restoring entry `1` puts `mode: 'local' # preview` back between `port` and `workers`. Neither step writes a file.

## How do you undo an edit to a YAML file before saving?

Keep the original on disk and edit the parsed document in memory. To reverse a single removal, keep the removed entry, the parent’s location, and the old index alongside your preview. The index is zero-based and tells the API where to insert **before** an entry. If the other entries were reordered after the removal, the original index may no longer put it between the same neighbors. Appending with `insert_removed` is a deliberate order change, as shown above.

An edit that drops the `Removed` can’t use this API to undo the deletion later. And this is not a file-wide undo history: to roll back after overwriting the source and discarding the original, you’d need another copy of the file.

## Can you restore a deleted YAML key with its comments?

Yes. Comments that belong to the entry are removed with it and come back when you reinsert it. The one-line note `# preview` and the single quotes around `local` come back in the run above. That’s different from an edit that deliberately leaves leading comments in the document, such as `remove_keep_comments`.

The [reinsertion API](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/crates/oneyaml/src/document/edit.rs#L8122-L8157) works within the same document; it doesn’t turn `Removed` into an export format for another file. It also rejects a duplicate key and can reject an invalid anchor or alias placement.
