PHP header_remove() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, header_remove() deletes a previously queued response header by name. If you omit the name, it removes all headers that PHP still controls for the response.

Call it before headers are sent. It is useful when a framework or earlier code set a header you do not want, such as a temporary debug header.

Syntax

Call header_remove() with the values you need. Start with this line.

PHP
header_remove("X-Powered-By");
CallWhat it does
header_remove("X-Debug")Removes the X-Debug header if it was queued.
header_remove("Content-Type")Clears the Content-Type header so you can set a new one.
header_remove()Removes all removable response headers.

Pass the header name without a colon. Matching is case-insensitive for the header name. Once headers_sent() is true, removal no longer affects what the client already received.

Library declarations
PHP
header_remove(?string $name = null): void

Parameters

ParameterDescription
nameOptional. The header name to remove, such as "X-Powered-By". Omit it to remove all removable headers.

Return Value

Return ValueDescription
voidNo return value.

PHP Examples

Example 1 — Remove One Queued Header

Set two headers, remove one, then show what remains in an illustrative headers_list() style listing.

PHP
<?php
header("X-One: keep");
header("X-Two: drop");
header_remove("X-Two");

foreach (["X-One: keep"] as $line) {
    echo $line, PHP_EOL;
}

How It Works

  1. Both X-One and X-Two are queued with header().
  2. header_remove("X-Two") deletes only that name.
  3. The printed line is what a web SAPI headers_list() would still show for X-One.
  4. CLI may show an empty headers_list(); the sample output is the web case.

Example 2 — Remove All Removable Headers

Call header_remove() with no argument to clear the queued set, then print a short status message.

PHP
<?php
header("X-A: 1");
header("X-B: 2");
header_remove();

echo "queued custom headers cleared", PHP_EOL;

How It Works

  1. Two custom headers are queued first.
  2. header_remove() with no name clears removable headers.
  3. We print a status line instead of relying on CLI headers_list().
  4. Use this carefully; you may also clear Content-Type you still need.

Example 3 — Replace Content-Type Cleanly

Remove the old Content-Type, set a new one, and list the result as a web SAPI would.

PHP
<?php
header("Content-Type: text/html");
header_remove("Content-Type");
header("Content-Type: application/json");

foreach (["Content-Type: application/json"] as $line) {
    echo $line, PHP_EOL;
}

How It Works

  1. The first Content-Type is HTML.
  2. header_remove() clears it before the JSON type is set.
  3. The final queued value is application/json.
  4. You could also call header() with replace true, which is the default.

Frequently Asked Questions

It deletes a queued response header by name, or all removable headers if you omit the name.
No. Pass only the name, such as "Content-Type" or "X-Debug".
No. Once headers_sent() is true, the client already received them.
Header names are matched without caring about letter case, so "content-type" and "Content-Type" refer to the same header.
header() sets or replaces a value. header_remove() deletes the header so it is not sent at all unless you add it again.

Next: headers_list()

headers_list() returns an array of response headers that are ready to send (or were prepar.

headers_list() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful