how to delete all cookies of my website in php

how to delete all cookies of my website in php

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

Stefan Bogdanescu

Founder & Senior Architect

The Mechanics of Deleting Cookies in PHP

You cannot simply pass false to setcookie() if your goal is to remove a cookie from the client's storage. In PHP, passing false as the value is a common misconception that stems from outdated documentation or misinterpretation of how the function handles values versus expiration. The setcookie() function does not distinguish between setting a new value and resetting a value; it treats them identically in terms of the HTTP header generation unless you explicitly provide an expiration time.

To effectively delete a cookie, you must instruct the browser to treat it as expired. This is achieved by setting the expires parameter to a timestamp that has already passed. When the browser receives a Set-Cookie header with an expiration date in the past, it marks that specific cookie for removal immediately upon loading the page.

Here is the standard implementation pattern:

deleteCookie($name) {
    $cookieName = str_replace(' ', '%20', $name); // Handle spaces if necessary
    setcookie(
        $cookieName, 
        '', 
        time() - 3600, // Set expiration to one hour ago
        '/',             // Path
        '',              // Domain (use same as script or empty)
        false,           // Secure (false for now, true for HTTPS only)
        false            // HttpOnly
    );
}

The critical element here is the third argument: time() - 3600. By setting the expiration time to a moment in the past, you trigger the browser's cleanup mechanism. It is also good practice to set the path and domain arguments exactly as they were when the cookie was originally set. If you omit these or change them, the browser will treat it as a distinct cookie entity rather than an update to the existing one.

The Logout Scenario

When implementing a logout function, you often need to ensure that session-based authentication tokens and user preferences are cleared. A naive approach might look like this:

// Incorrect: This does not delete the cookie
setcookie("user_session", false); 
session_destroy();

While session_destroy() removes data from the server-side storage, it does not automatically instruct the client to discard cookies that might contain sensitive tokens or persistent identifiers. Even if session_destroy() is called, a malicious user could retain their session cookie in an insecure context or use it for cross-site request forgery (CSRF) attacks until it naturally expires.

To secure a logout flow, you must explicitly clear the relevant cookies:

// Clear specific cookies by setting expiration to the past
setcookie('user', '', time() - 3600, '/');
setcookie('preferences', '', time() - 3600, '/');
session_destroy();

This ensures that when the user refreshes the page or navigates back to your site, the browser will send no cookies associated with those names.

Handling Domain and Path Scope

Your source question asked about deleting "all" cookies for a website. In PHP, there is no direct API call like deleteAllCookies(). You must iterate over the specific cookies you wish to remove. However, if you are using a framework like Laravel, you can leverage its helpers or manually clear them.

If your application uses strict security practices, you might want to ensure that all cookies with a specific domain are cleared. Since PHP's setcookie() function allows you to specify the domain and path, you must match these exactly to the original cookie definition. If a cookie was set with Domain=.example.com and Path=/, your deletion logic must include those same parameters.

// Example of clearing a cookie set with specific attributes
setcookie(
    'tracking_id', 
    '', 
    time() - 3600, 
    '/',           // Path
    '.example.com' // Domain - note the leading dot for subdomains
);

Common Pitfalls and Limitations

One frequent error occurs when developers try to delete cookies that were never set with HttpOnly or Secure flags. While PHP's setcookie() function technically allows you to override these flags during deletion, browsers may enforce stricter policies depending on their version. For instance, if a cookie was originally set as Secure, your deletion request must also include the Secure flag; otherwise, the browser will ignore the deletion header entirely for security reasons.

Another limitation is that PHP cannot delete cookies belonging to third-party domains or other subdomains unless you explicitly specify the full domain name in the setcookie() call. You cannot delete a cookie set by analytics.example.com from your script at main.example.com without knowing its exact attributes and specifying them correctly.

Finally, remember that deleting a cookie does not immediately wipe the data from the browser's disk if it is stored in LocalStorage or IndexedDB. These storage mechanisms are separate from HTTP cookies. If your application relies on both, you must handle the cleanup of local storage separately to fully secure the user's state after logout.

For developers working within the Laravel ecosystem, ensure you check your session configuration and middleware to guarantee that session_destroy() is coupled with explicit cookie clearing logic whenever a user logs out.

Our network