PHP stream_wrapper_unregister() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_wrapper_unregister() removes a protocol scheme from the list of active stream wrappers.

Use it to drop a custom scheme you registered, or to temporarily remove a built-in scheme before installing a replacement. It returns true on success. Restore built-ins later with stream_wrapper_restore().

Syntax

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

PHP
$ok = stream_wrapper_unregister('demo');
CallWhat it does
stream_wrapper_unregister('demo')Removes the custom demo:// scheme.
stream_wrapper_unregister('http')Temporarily removes the built-in http wrapper.
stream_wrapper_unregister($protocol)Unregisters the scheme name stored in $protocol.

After unregister, fopen('demo://...') will fail for that scheme. Built-in schemes can be brought back with stream_wrapper_restore(). Custom schemes need stream_wrapper_register() again.

Library declarations
PHP
stream_wrapper_unregister(string $protocol): bool

Parameters

ParameterDescription
protocolRequired. The scheme name to remove, without ://.

Return Value

Return ValueDescription
trueThe protocol was unregistered.
falseThe protocol could not be removed.

PHP Examples

Example 1 — Register Then Unregister

Register a stub scheme, unregister it, and show it is gone from the wrappers list.

PHP
<?php
class GoneStream {
    public $context;
    public function stream_open($path, $mode, $options, &$opened_path) { return true; }
    public function stream_read($count) { return ''; }
    public function stream_eof() { return true; }
    public function stream_stat() { return []; }
}

stream_wrapper_register('gone', GoneStream::class);
stream_wrapper_unregister('gone');
echo in_array('gone', stream_get_wrappers(), true) ? 'still there' : 'removed', PHP_EOL;

How It Works

  1. Register the gone protocol with a stub class.
  2. stream_wrapper_unregister('gone') removes it.
  3. stream_get_wrappers() no longer lists gone.
  4. Later fopen('gone://x') would fail.

Example 2 — Boolean Result of Unregister

Print success when unregistering a scheme you just added.

PHP
<?php
class DropStream {
    public $context;
    public function stream_open($path, $mode, $options, &$opened_path) { return true; }
    public function stream_read($count) { return ''; }
    public function stream_eof() { return true; }
    public function stream_stat() { return []; }
}

stream_wrapper_register('drop', DropStream::class);
$ok = stream_wrapper_unregister('drop');
echo $ok ? 'unregistered' : 'failed', PHP_EOL;

How It Works

  1. Register drop:// first so unregister has a target.
  2. unregister returns true on success.
  3. false means the scheme was missing or could not be removed.
  4. Use the result in tests and setup/teardown code.

Example 3 — Built-in Reminder

Document that built-in schemes should be restored after a temporary unregister.

PHP
<?php
// If you unregister a built-in such as http during a test:
// stream_wrapper_unregister('http');
// ... temporary replacement ...
// stream_wrapper_restore('http');
echo 'custom schemes: unregister is enough', PHP_EOL;
echo 'built-ins: restore afterward', PHP_EOL;

How It Works

  1. Custom schemes only need unregister when finished.
  2. Built-ins should be restored for the rest of the request.
  3. stream_wrapper_restore() brings the original handler back.
  4. Avoid leaving core schemes disabled in production.

Frequently Asked Questions

It removes a protocol scheme so PHP no longer routes that URL prefix through a stream wrapper.
Call stream_wrapper_register() again with your class name.
Call stream_wrapper_restore() with the protocol name.
Existing open streams may continue until closed, but new opens for that scheme fail. Prefer clean setup/teardown around tests.
The protocol was not registered, or PHP could not remove it.

Back to Stream Functions

Browse all PHP stream function tutorials.

Stream functions →

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