PHP ob_clean() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Output Control

Definition

In PHP, ob_clean() erases the contents of the active output buffer but keeps buffering turned on.

After a clean, new echo calls go into the same buffer again. Use this when you want to throw away draft output and continue capturing. To erase and turn the buffer off, use ob_end_clean() instead.

Syntax

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

PHP
$ok = ob_clean();
CallWhat it does
ob_clean()Clears the active buffer contents and returns true on success.
ob_start(); echo "x"; ob_clean();Removes "x" from the buffer; buffering continues.
ob_get_length()Often 0 right after a successful ob_clean().

ob_clean() needs an active buffer that allows cleaning. With no buffer, it fails and returns false (and may raise a notice). It does not send anything to the client.

Library declarations
PHP
ob_clean(): bool

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
booltrue on success, false on failure (for example when no active buffer can be cleaned).

PHP Examples

Example 1 — Replace Buffer Contents

Write one string, clean the buffer, write another, then capture what remains.

PHP
<?php
ob_start();
echo "first";
ob_clean();
echo "second";
$text = ob_get_clean();
echo $text, PHP_EOL;

How It Works

  1. ob_start() begins buffering.
  2. "first" is written, then ob_clean() erases it.
  3. "second" is written into the same still-active buffer.
  4. ob_get_clean() returns "second", ends the buffer, and we print it.

Example 2 — Length Drops to Zero

Check ob_get_length() before and after ob_clean() to see the buffer empty while the level stays up.

PHP
<?php
ob_start();
echo "abc";
$before = ob_get_length();
$levelBefore = ob_get_level();
ob_clean();
$after = ob_get_length();
$levelAfter = ob_get_level();
ob_end_clean();
echo "len ", $before, "->", $after, " level ", $levelBefore, "->", $levelAfter, PHP_EOL;

How It Works

  1. Three characters are buffered, so the length is 3.
  2. ob_get_level() is 1 while that buffer is active.
  3. ob_clean() clears the text; length becomes 0.
  4. The level stays 1 because cleaning does not turn buffering off.

Example 3 — Capture an Empty String After Clean

Clean the buffer, then use ob_get_contents() to show that the copy is empty while buffering continues.

PHP
<?php
ob_start();
echo "discard me";
ob_clean();
$copy = ob_get_contents();
$level = ob_get_level();
ob_end_clean();
echo "copy=", var_export($copy, true), " level=", $level, PHP_EOL;

How It Works

  1. Text is written into the buffer.
  2. ob_clean() removes that text.
  3. ob_get_contents() returns an empty string because nothing is left.
  4. ob_get_level() is still 1, then ob_end_clean() ends the buffer.

Frequently Asked Questions

ob_clean() erases the contents of the active output buffer but leaves buffering on so later echo calls are captured again.
ob_clean() only clears the text. ob_end_clean() clears the text and turns the top buffer off, lowering ob_get_level() by one.
No. It discards the buffered text. To send buffer contents, use ob_flush() or ob_end_flush().
It fails and returns false. PHP may also emit a notice. Start a buffer with ob_start() first.
Use it when you buffered something by mistake (or as a draft) and want a clean buffer without ending the buffering session.

Next: ob_end_clean()

ob_end_clean() erases the contents of the active output buffer and turns that top buffer o.

ob_end_clean() 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