How to Delete a Cookie With PHP

By | July 9, 2012

Working with cookies is essential when you create websites, that track user activity on the site. Sometimes you need to delete cookies you previously set. Since cookies are small files that are stored on user computers, you don’t have the ability to remove them as files. But you can delete them using a simple PHP script.


setcookie(“partner”, “”, time()-3600);


That’s a very simple yet effective trick. You don’t delete the cookie file itself, but you update the cookie value and make it expired.

We’re using setcookie function, that accepts 3 arguments (there may be some more arguments, but those essential to delete a cookie are mentioned below).

First argument represents the name of the cookie variable. Please, replace “partner” with your cookie name. The second argument passed to setcookie function is blank, so you just delete the value of your cookie variable. And finally the third argument sets the cookie expiration date to the past (current time-1 hour).

This way the cookie file on the user side will be updated and if fact cookie variable will not be valid anymore.

That’s a good method to delete a cookie variable, when you know its name. What to do if you just want to delete all user cookies, but are not sure about their names? Here comes another simple script:


foreach ($_COOKIE as $cookiename=>$cookievalue)
setcookie($cookiename, “”, time()-3600);


The code above will clear all the cookies stored. You might need this code when you need to process user logout, or to reset any other user-defined options