Keep the removed entry and pass it to insert_removed_at to undo the deletion.
cargo add oneyamluse 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, then cargo run.
Requires Rust 1.85 or later. The snippet prints the preview and the restored YAML:
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.
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:
Preview: port: 8080 workers: 2 Restored: port: 8080 mode: 'local' # preview workers: 2 Appended: port: 8080 workers: 2 mode: 'local' # preview
Related recipes
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 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.