# Remove comments (remove_comments)

Strips HTML comments. How the mod_pagespeed remove_comments filter works, when to use it, its risks and the Apache, nginx and IIS directives.

Canonical URL: https://modpagespeed.com/docs/filters/remove_comments/

- Category: HTML
- CoreFilters: No
- OptimizeForBandwidth: No
- Risk: Generally safe

`remove_comments` deletes HTML comments (`<!-- ... -->`) from the served page, cutting bytes that only developers read. The document tree is unchanged apart from the removed nodes. Live demo: [remove_comments](https://modpagespeed.com/examples/remove_comments/).

## How it works

### When it helps and when it does not

It helps on pages whose templates carry heavy commentary: build markers, TODO notes, and section labels add up on large documents. It does nothing for pages that ship few comments, and it must not remove comments that carry a function. Comments you are obliged or willing to ship stay behind a `RetainComment` wildcard, and IE conditional comments (`<!--[if IE]> ... <![endif]-->`) are parsed as directives rather than comments, so they always survive.

### How it decides

Every comment node is dropped unless its text matches one of the configured `RetainComment` wildcard patterns. There is no size threshold and no content analysis beyond the pattern match.

## When to use it

- Not in CoreFilters: it runs only when you enable it by name.
- Risk rating on these docs: **Generally safe**.

## Risks

- Copyright or license notices that must ship with the page need a `RetainComment` entry before the filter goes on.

- A rare third-party snippet that reads the page's own comments breaks; retain its marker comment or disable the filter for that path. Verify with the `X-Mod-Pagespeed` header and `?PageSpeedFilters=-remove_comments`; [Is it working?](https://modpagespeed.com/docs/is-it-working/) has the steps.

## Configuration

Enable it in the module configuration, at server, virtual-host or location scope:

**Apache**

```apache
ModPagespeedEnableFilters remove_comments
```

**nginx**

```nginx
pagespeed EnableFilters remove_comments;
```

**IIS (pagespeed.config)**

```text
pagespeed EnableFilters remove_comments
```

```html
<!-- before -->
<!-- TODO: replace with the component version -->
<div class="banner">Sale ends Friday</div>

<!-- after -->
<div class="banner">Sale ends Friday</div>
```

```apache
# Apache
ModPagespeedEnableFilters remove_comments
ModPagespeedRetainComment "*copyright*"
```

```nginx
# Nginx
pagespeed EnableFilters remove_comments;
pagespeed RetainComment "*copyright*";
```

Scoping, `ForbidFilters` and the thresholds filters read: [Choosing filters](https://modpagespeed.com/docs/filter-selection/).

## Live example

See the before and after, with the source diff: [remove_comments example](https://modpagespeed.com/examples/remove_comments/).

## Frequently asked questions

**What does the remove_comments filter do?**

`remove_comments` deletes HTML comments (`<!-- ... -->`) from the served page, cutting bytes that only developers read. The document tree is unchanged apart from the removed nodes. Live demo: remove_comments.

**Is remove_comments enabled by default?**

No. `remove_comments` is not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name with `EnableFilters`.

**How do I enable remove_comments on Apache and nginx?**

Add `ModPagespeedEnableFilters remove_comments` on Apache or `pagespeed EnableFilters remove_comments;` on nginx. On IIS, add `pagespeed EnableFilters remove_comments` to pagespeed.config.

## Related filters

- [`add_base_tag`](https://modpagespeed.com/docs/filters/add_base_tag/): Add base tag
- [`add_head`](https://modpagespeed.com/docs/filters/add_head/): Add head
- [`add_ids`](https://modpagespeed.com/docs/filters/add_ids/): Add IDs
- [`add_instrumentation`](https://modpagespeed.com/docs/filters/add_instrumentation/): Add instrumentation
- [`collapse_whitespace`](https://modpagespeed.com/docs/filters/collapse_whitespace/): Collapse whitespace
- [`combine_heads`](https://modpagespeed.com/docs/filters/combine_heads/): Combine heads
- [`compute_statistics`](https://modpagespeed.com/docs/filters/compute_statistics/): Compute HTML statistics
- [`convert_meta_tags`](https://modpagespeed.com/docs/filters/convert_meta_tags/): Convert meta tags

This page is drawn from the [remove_comments entry on HTML filters](https://modpagespeed.com/docs/html-filters/#remove_comments). Every filter in one table: [PageSpeed filters](https://modpagespeed.com/docs/filters/).
