// formatters

Remove YAML Comments.

Remove all comments and blank lines from a YAML document while preserving structure and quoting.

Client-sideQuote-awareNo upload

YAML input

Stripped

Why remove YAML comments?

Comments are useful during development but can cause problems in production. Some tools don't preserve comments when parsing and re-serializing YAML. If you're sending YAML data to an API, embedding it in a config file that doesn't support comments, or debugging a parse error, removing comments first gives you a clean, valid document.

This tool preserves the YAML structure exactly — only comment lines and inline comments are removed. The output is syntactically identical to the input, minus the comments. It handles both full-line comments (# comment) and inline comments (value # comment).

Comment handling details.

Full-line comments. Lines that start with # (possibly preceded by whitespace) are removed entirely. Blank lines between comments are preserved to maintain spacing. Inline comments. Comments at the end of a value line (key: value # comment) have the comment stripped, leaving just key: value.

Quoted strings. Comments inside quoted strings are not removed. The string "Hello # world" keeps the # because it's inside quotes. Only unquoted # characters are treated as comment markers.

When to use this tool.

Production configs. Some deployment tools strip comments automatically but may do so incorrectly. Use this tool to remove comments cleanly before deploying. Data pipelines. When piping YAML between tools, comments can cause unexpected behavior. Strip them before processing. Minification. Removing comments and blank lines reduces YAML file size for transmission or storage.

Why comments cause problems.

YAML parsers handle comments inconsistently. Some parsers discard comments during parsing, so re-serializing the data loses them. Others preserve comments but may alter whitespace or ordering. When YAML data passes through multiple tools in a pipeline, comments can trigger parse errors or silent data loss. Removing comments upfront avoids these issues entirely and ensures consistent output across different tools and environments.

Comments can also leak information. A commented-out configuration line might reveal internal hostnames, credentials, or debug settings. In production, stripping comments ensures that only intentional, documented configuration reaches your deployment environment.

How the stripper works.

The tool processes the YAML line by line. For each line, it identifies unquoted # characters and removes everything from that point to the end of the line. Quoted strings are scanned character by character to avoid false positives — a # inside "value" or 'value' is preserved. Lines that become empty after stripping (full-line comments) are removed entirely, but consecutive blank lines are collapsed to a single blank line to maintain readability. This approach ensures clean, production-ready YAML output.

Edge cases to watch for.

YAML flow scalars that contain # after a space are treated as comments by the parser. For example, { key: value # note } has an inline comment, but { key: value#note } does not — the # is part of the value. This tool handles both cases correctly by checking whether the # is preceded by whitespace. If you're working with flow scalars, review the output to confirm the intended values are preserved. The tool works with both block and flow scalar styles, producing clean output regardless of the original formatting.

FAQ

How do I strip all comments from a YAML file from the command line?

`yq -I=0 '... comments=""' file.yaml` outputs without comments; or `sed -E 's/\s*#.*$//' file.yaml` for a quick (but naive) text-level strip.

How do I remove comments without breaking my YAML structure?

Use a parser-based tool (`yq`, `ruamel.yaml` round-trip, `prettier`) — naive `sed` will mangle comments inside strings or after escaped chars.

Can I keep some comments but remove others in YAML?

No easy way — comment removal is all-or-nothing in most tools; manually edit, or write a `ruamel.yaml` script that walks comments and deletes specific ones.

Why do my YAML comments reappear after some tools process the file?

Tools that parse-and-dump YAML (like `yq`) drop comments because YAML comments don't survive the round-trip to a data structure and back.

Is there a regex to safely remove YAML comments?

A simple one: `^\s*#.*$` removes full-line comments; for inline `#` after a value, you need a parser because `#` inside strings must NOT be stripped.