---
title: "Remove comments attached to a YAML key in Rust"
url: https://recursiveai.org/oneyaml/rust/cookbook/clear-yaml-node-comments/
description: "Remove the comments attached to one YAML key in Rust with clear_comments. Keep the key and its value, and see the comment text that was removed."
version: "1.2.0"
---

# Remove comments attached to a YAML key 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/clear-yaml-node-comments/clear-yaml-node-comments.zip)

Use `clear_comments` to remove the note above `port` and the note beside it, while keeping `port: 443`.

```shell
cargo add oneyaml
```

Requires Rust 1.85 or later.

```rust
use oneyaml::Document;

fn main() {
    let yaml = "# Old note\nport: 443 # TLS\n";
    let mut doc = Document::parse(yaml)
        .expect("valid YAML");
    let mut port = doc.get_mut("port")
        .expect("port");
    let removed = port.clear_comments()
        .expect("clear comments");
    print!("{doc}");
    println!("Removed: {removed:?}");
}
```

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

`cargo run` prints:

```text
port: 443
Removed: ["# Old note", "# TLS"]
```



## Variations

To remove only the same-line note, replace everything after the `let mut port` statement with this code. It prints `# Old note` above `port: 443`.

```rust
port.set_trailing(None)
    .expect("remove inline note");
print!("{doc}");
```

## Related recipes

- [Replace comments above a YAML key](https://recursiveai.org/oneyaml/rust/cookbook/set-leading-yaml-comments/)
- [Find which YAML node owns a comment](https://recursiveai.org/oneyaml/rust/cookbook/explain-yaml-comment-owner/)

## The short answer

Parse the YAML as a `Document`, select the entry with `get_mut`, and call `clear_comments`. It edits the YAML and returns the removed comment strings. The example prints the remaining YAML first, then the removed text, including the `#` markers.

## How do you remove YAML comments without deleting the key?

Call `clear_comments`, not `Document::remove_node`, which removes the mapping entry. The key and value stay in place. [`Document::clear_comments`](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/crates/oneyaml/src/document/comment_edit.rs#L1313-L1333) takes a node ID and acts on exactly that node. The [mutable entry handle](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/crates/oneyaml/src/document/edit.rs#L8520-L8532) used in the example also clears comments owned by the value itself. For example, if the value is a list, the handle can clear the list’s footer as well as the notes on the key.

Neither method walks down into all child entries to strip their comments. If you choose one key in a larger mapping, it isn’t a request to erase the notes on other keys or the document’s header and footer. If the edit would make another comment belong to a different node, it returns `EditError::CommentReattaches` and changes nothing.

## How do you remove an inline YAML comment but keep the note above it?

Use `set_trailing(None)`, as in the variation. A same-line comment is called a *trailing* comment in the API. This operation targets that slot, so the leading note remains. To do the opposite, use `set_leading` with `NO_COMMENTS`.

Avoid deleting everything after a `#` by string replacement. For instance, in `color: "#0055aa"`, the `#` is inside a [quoted value](https://yaml.org/spec/1.2.2/#3233-comments), not a YAML comment. Editing the selected entry’s parsed comments leaves that value alone.
