How to remove a cookie in PHP?

How to remove a cookie in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Illusion of unset() in PHP Cookies

A common stumbling block for developers working with session state is the belief that unset($_COOKIE['hello']) will effectively delete a cookie from the client's browser. While this syntax successfully removes the entry from the server-side superglobal array, it has absolutely no impact on the actual HTTP cookie stored on the user's device. The reason lies in how the Cookie header operates within the HTTP protocol and how PHP handles $_COOKIE internally.

The $_COOKIE variable is a read-only snapshot of cookies that were sent to the server during the current request. It does not represent a live connection to the client's storage. When you execute unset($_COOKIE['hello']), you are merely clearing your application's memory of that specific cookie for the duration of this single request cycle. Once the script finishes executing and sends its response headers, the PHP process terminates, and the in-memory array is discarded. The browser, however, remains completely unaware of your server-side manipulation. It still possesses the original cookie string, including its expiration date and domain attributes, ready to send it with the next request.

To actually remove a cookie on the client side, you must instruct the browser to overwrite the existing cookie with an expired version. This is achieved by sending a Set-Cookie header in your response that mirrors the original cookie's properties—specifically its name, domain, path, and security flags—but sets the expiration date to a time in the past.

Consider a scenario where you want to delete a cookie named user_preference. If you attempt to rely solely on unset($_COOKIE['hello']), the browser will continue to recognize it. The correct implementation involves using the setcookie() function before any output is sent to the browser. You must explicitly pass an expiration timestamp that is strictly less than the current time, such as one minute ago.

// Attempting to delete a cookie
$cookieName = 'user_preference';
$domain = ''; // Use empty string for default domain, or specify if set
$path = '/';  // Use '/' for root path, or specify if different

setcookie($cookieName, '', [
    'expires' => time() - 3600,
    'path' => $path,
    'domain' => $domain,
    'secure' => false, // Set to true only if the original cookie was secure and served over HTTPS
    'httponly' => true, // Preserve original attributes for security consistency
    'samesite' => 'Lax' // Preserve original SameSite attribute if possible
]);

The critical detail here is that setcookie() sends an HTTP response header. The browser receives this header and updates its internal store, replacing the valid cookie with one that has already expired. On subsequent requests, the browser will not send this deleted cookie back to your server because it considers it invalid. It is important to note that you should generally preserve the httponly and secure flags from the original cookie definition to avoid mixed content warnings or security regressions.

In a Laravel application, this logic is often handled via helpers or middleware, but understanding the underlying mechanism remains essential for debugging. If you are using Laravel's Cookie facade, the framework provides a convenient method to handle this abstraction:

use Illuminate\Support\Facades\Cookie;

// Remove a cookie in Laravel
Cookie::queue(Cookie::forget('user_preference'));

Under the hood, Cookie::forget() executes a setcookie() call with an expired timestamp, ensuring the browser is correctly instructed to drop the token. Relying on unset($_COOKIE['key']) alone is a frequent source of debugging nightmares where users report that their logout functionality or preference resets are not working as expected. The solution always requires sending a valid HTTP response header to the client, effectively overwriting the existing state with an expired value.

Our network