---
title: "Delete a YAML key in Go and keep comments"
url: https://recursiveai.org/oneyaml/go/cookbook/remove-yaml-key-without-reformatting/
description: "Remove a nested YAML key in Go without reformatting the remaining entries. See which comments are removed and which stay."
version: "1.0.0"
---

# Delete a YAML key in Go and keep comments

**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/remove-yaml-key-without-reformatting/remove-yaml-key-without-reformatting.zip)

Call `doc.Remove("/service/port")`. The deleted key’s comments go with it; the next key’s comment stays.

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

Requires Go 1.24+. Run in a Go module.

```go
package main

import (
	"fmt"

	"oneyaml.dev/go"
)

func main() {
	input := []byte(`service:
  # Old port
  port: 80 # remove this too
  # Public hostname
  host: 'example.org'
`)
	doc, err := oneyaml.Parse(input)
	if err != nil {
		panic(err)
	}
	err = doc.Remove("/service/port")
	if err != nil {
		panic(err)
	}
	fmt.Print(doc)
}
```

Unzip [the example](https://recursiveai.org/oneyaml/go/cookbook/remove-yaml-key-without-reformatting/remove-yaml-key-without-reformatting.zip), then `go run .`.

Prints the edited YAML:

```text
service:
  # Public hostname
  host: 'example.org'
```

If the path doesn’t exist, `Remove` returns an error that matches `ErrNotFound` and leaves the document unchanged.

## Variations

**Build a path from key names.** In place of the `Remove` assignment and its error check, use `Pointer` to join and escape the names. This produces the same output for the sample. It also handles literal `/` and `~` in a key name: in a JSON Pointer, `/` separates keys and `~` is the escape character.

```go
ptr := oneyaml.Pointer("service", "port")
if err := doc.Remove(ptr); err != nil {
	panic(err)
}
```

**Edit a file on disk.** Save the input YAML from the snippet above as `config.yaml`. Add `"os"` to the imports, remove `"fmt"`, and replace the body of `main` with this code. It writes the same edited YAML shown above to `updated.yaml` and leaves `config.yaml` alone.

```go
input, err := os.ReadFile("config.yaml")
if err != nil {
	panic(err)
}
doc, err := oneyaml.Parse(input)
if err != nil {
	panic(err)
}
if err := doc.Remove("/service/port"); err != nil {
	panic(err)
}
err = os.WriteFile("updated.yaml", doc.Bytes(), 0644)
if err != nil {
	panic(err)
}
```

## Related recipes

- [Edit a YAML file and keep its comments](https://recursiveai.org/oneyaml/go/cookbook/edit-yaml-keep-comments/)
- [Rename a YAML key without losing comments](https://recursiveai.org/oneyaml/go/cookbook/rename-yaml-key-keep-comments/)

## The short answer

Parse the YAML into a document, then use `Remove` with a JSON Pointer such as `/service/port`. A pointer is a path from the root, so this one removes only `port` under `service`. The edited text is available as `doc.Bytes()`. Printing the document lets you review the deletion before writing anything to disk.

## Which comments stay when you delete a YAML key?

In the example, `# Old port` is directly above `port`, and `# remove this too` is on the same line. Both are removed with the entry. The comment `# Public hostname` is above the next key and stays. The [editing guide](https://recursiveai.org/oneyaml/go/docs/editing/#adding-and-removing) also specifies that comments inside the removed value go with it. That matters when the value is itself a mapping or list: `Remove` deletes the whole entry, not just the key’s text.

Before deleting a setting, check whether the comment above it is really about that setting. If it explains the whole section and you want to keep it, move it before removing the entry. If the setting is still needed but its name has changed, rename it instead; that keeps the entry and its comments in place.

## How do you edit YAML without reformatting the rest of the file?

Edit the parsed document instead of unmarshaling into a Go map and marshaling it again. A plain Go map holds values, not details like the original quotes or the comments people left for each other. Here the hostname still has its original single quotes and indentation. For a file on disk, read its contents as the input to `Parse` and, after a successful edit, save the bytes from `doc.Bytes()`.

Use a pointer to specify the exact entry, especially if a name such as `port` occurs under several parents. The empty pointer selects the whole document, and `Remove` refuses to delete the root. To remove a setting rather than clear its value, use `Remove`, not `Set` with `nil`: a key set to YAML `null` still exists.
