How to delete cookie with HttpOnly using PHP or JS

How to delete cookie with HttpOnly using PHP or JS

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

Stefan Bogdanescu

Founder & Senior Architect

The Mechanics of Deleting HttpOnly Cookies

Deleting an HttpOnly cookie requires understanding that the "HttpOnly" flag is a security restriction, not a storage mechanism. It prevents client-side scripts like JavaScript from accessing the cookie via document.cookie, but it does not alter how the browser stores or retrieves the data. Consequently, you cannot delete an HttpOnly cookie using JavaScript. To clear this specific cookie, you must rely on server-side logic, specifically PHP, and ensure every attribute matches the original creation parameters exactly.

The Critical Role of Exact Attribute Matching

The core issue in your scenario stems from a mismatch between how the browser stored the cookie and how the setcookie() function is attempting to delete it. When a cookie is set with specific attributes—domain, path, secure flag, and httponly flag—the browser creates a unique entry for that cookie. To delete it, you must issue a Set-Cookie header (or call setcookie()) that is identical to the original creation command, except for the expiration time.

If any attribute differs, the browser treats the new instruction as a request for a different cookie and ignores the deletion attempt entirely. In your case, the original cookie has the following configuration:

  • Name: workgroup_session_id
  • Domain: agrobman1.tsi.lan
  • Path: /
  • Secure: No (default)
  • HttpOnly: Yes

Your PHP attempt used the correct expiration logic (time() - 3600), but you inadvertently changed the domain and path parameters. You passed "agrobman1.tsi.lan" as the domain argument, which is actually correct for that specific cookie, but let's look closer at the setcookie() signature. The function signature is setcookie(name, value, expire, path, domain, secure, httponly).

In your code snippet:

setcookie("workgroup_session_id", "\"\"", time() - 3600, "/", "agrobman1.tsi.lan", false, true);

You are passing false for the $secure flag and true for the $httponly flag. This part is technically correct based on the source material which states "Send for: Any kind of connection" (implying non-secure) and "Accessible to script: No". However, there is a subtle but fatal trap often overlooked: the path.

The Path Trap

The most common reason setcookie() fails silently when deleting HttpOnly cookies is an incorrect path parameter. Your source material shows the cookie was created with Path: /. This means the browser expects the cookie to be sent for every request on that domain. If your application runs from a subdirectory, say /app, and you set the path to /app when deleting a cookie originally scoped to /, the deletion will fail. The browser will simply send the old cookie alongside the new one because they are considered distinct entities.

Ensure your PHP script is executed in an environment where the request path aligns with the cookie's original scope. If the cookie was created globally (/), your setcookie() call must also explicitly specify / as the path, which you have done correctly in your example. However, double-check that no other code on the page has set a different default path via configuration or previous scripts.

Why JavaScript Cannot Do This

You asked if JS can delete it. The answer is a definitive no. The HttpOnly attribute was introduced specifically to mitigate Cross-Site Scripting (XSS) attacks. By making the cookie value inaccessible to document.cookie, JavaScript loses the ability to read, modify, or delete it via standard APIs like document.cookie = "".

Attempting to access document.cookie in a browser console will return an empty string or throw an error for this specific cookie. There is no undocumented method or hack to bypass the HttpOnly flag from the client side without compromising the security model of the web application. If you need to invalidate this session, you must send a response that tells the PHP backend to issue the deletion header.

Best Practices for Session Cleanup

When deleting cookies to clear sessions, always use an explicit expiration time in the past. Setting the value to "" is optional but often included; however, the expiration date is the mandatory part of the deletion instruction. Use a timestamp that has already passed:

$expire = time() - 3600; // Expires one hour ago
setcookie("workgroup_session_id", "", $expire, "/", "agrobman1.tsi.lan", false, true);

This command instructs the browser: "Forget any cookie named workgroup_session_id that lives on agrobman1.tsi.lan at path / and is marked as HttpOnly." If the browser receives this instruction, it will scrub the entry from its internal store immediately.

If you are building a larger application where session management is critical, consider using a dedicated session handling library or framework features that abstract these details away. For instance, Laravel provides robust methods for clearing cookies and managing sessions without worrying about the underlying setcookie signature, ensuring attributes like HttpOnly and Secure are handled consistently throughout your stack.

Our network