Call doc.Remove("/service/port"). The deleted key’s comments go with it; the next key’s comment stays.
go get oneyaml.dev/goRequires Go 1.24+. Run in a Go module.
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, then go run ..
Prints the edited YAML:
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.
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.
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
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 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.