ob_end_clean
Discards the contents of the active output buffer and turns that buffer off.
ob_end_clean(): bool- Added in PHP 4.0
Return value
boolReturns true on success or false on failure.
Errors
Fails and emits an E_NOTICE when there is no active output buffer, or when the active buffer was started without the PHP_OUTPUT_HANDLER_REMOVABLE flag.
ob_end_clean() calls the active buffer's handler with the PHP_OUTPUT_HANDLER_CLEAN and PHP_OUTPUT_HANDLER_FINAL flags, throws away both the handler's return value and the buffer contents, then removes the buffer from the stack. Nothing is printed and nothing is returned to you.
When to reach for it
It is the standard way to silence code you do not control. Wrap a library call that insists on echoing warnings or debug output in ob_start() and ob_end_clean() and only its return value survives. It is equally the standard cleanup in an error handler: unwind every buffer opened so far so a half-rendered page never reaches the browser, then emit a clean error response.
What trips people up
It closes exactly one level. If your code opened three buffers, or if output_buffering is enabled in php.ini so a buffer exists before your script starts, a single call leaves output still trapped. Record ob_get_level() at the point you want to return to and loop back down to it.
Note also that it discards buffer contents even when the buffer was created without PHP_OUTPUT_HANDLER_CLEANABLE; only the missing PHP_OUTPUT_HANDLER_REMOVABLE flag makes it fail.
Example
When to use it
- Silence a legacy library that echoes warnings, keeping only its return value.
- Unwind partially rendered output in an exception handler before sending a clean error page.
- Run a template purely for its side effects (setting variables) and throw the markup away.
Frequently asked questions
What is the difference between ob_end_clean and ob_end_flush?
Both close the active buffer, but ob_end_clean() throws the contents away while ob_end_flush() sends them on to the parent buffer or the client.
How do I close all output buffers at once?
Loop: while (ob_get_level() > 0) { ob_end_clean(); }. If output_buffering is on in php.ini, stop at the level your script started with rather than at 0.
Why does ob_end_clean return false?
There was no active buffer, or the buffer was started without the PHP_OUTPUT_HANDLER_REMOVABLE flag, which some handlers deliberately omit so they cannot be removed early.
More examples
Nothing is printed, only the boolean result
The output is only 'buffer discarded: bool(true)'. The echoed sentence went into the buffer and was destroyed with it.
<?php
ob_start();
echo "This text never reaches the client.\n";
$discarded = ob_end_clean();
echo "buffer discarded: ";
var_dump($discarded);Unwinding every buffer back to a known level
Neither fragment appears. The loop closes both buffers regardless of how many were open, and the var_dump confirms bool(true): the stack is back where the script found it.
<?php
$base = ob_get_level();
ob_start();
echo "outer fragment";
ob_start();
echo "inner fragment";
// Something went wrong: drop everything we rendered.
while (ob_get_level() > $base) {
ob_end_clean();
}
echo "clean error page\n";
var_dump(ob_get_level() === $base);
Discussion