I've created a cookie as shown and append any newly added data to the cookie:

if (cart == null) {
               //Create an instance of the cookieJar class, passing it the values entered in the fields
                cookieJar c = new cookieJar(p.productid, selSize, selColour, String.valueOf(p.qtyToBuy));          
            } else {   //Append cookie
               String pid = cart.getValue();
               String psize = cart2.getValue();
               String pqty = cart3.getValue();
               String pcol = cart4.getValue();

               cookieJar c = new cookieJar(p.productid + ',' + pid, selSize + ',' + psize, selColour + ',' + pcol ,String.valueOf(p.qtyToBuy) + ',' + pqty);


I would like to delete the cookie data based on the productid. Is there way to do so? I understand from documentation that After you create a cookie, the properties of the cookie can't be changed. Does that mean that I can't delete the cookie?

On the other hand, I've tried using Javascript on my VF page to delete the particular cookie with product id, but it doesn't seems to work.

        <apex:column headerValue="Product" value="{!item.Name}"/>
       <apex:column headerValue="Colour" value="{!item.Color}"/> 
       <apex:column headerValue="Size" value="{!item.Size}"/> 
       <apex:column headerValue="Quantity">
            <apex:outputText value="{!item.qtyToBuy}" />

          <apex:column headerValue="Remove">
          <apex:commandLink action="{!removecon}" reRender="s" >
          <apex:param assignTo="{!conid}" value="{!item.productid}" name="assignvalue" />Remove </apex:commandLink> 
        <apex:actionSupport event="onclick" oncomplete="deletecook();" reRender="s" />


function deletecook() {
document.cookie = "productid='{!conid}'; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";

Cookie Jar Class

public class cookieJar {

        // The controller extension does the work
        public cookieJar(String productid, String productsize, String productcolour, String qtyToBuy) {

            Cookie pId = new Cookie('productid', productid,null,-1,false);
            Cookie pSize = new Cookie('productSize', productsize,null,-1,false);
            Cookie qty = new Cookie('qtyToBuy', qtyToBuy,null,-1,false);
            Cookie pCol = new Cookie('productColour', productcolour,null,-1,false);

            //Set the page cookies using the setCookies() method
            ApexPages.currentPage().setCookies(new Cookie[]{pId, pSize, pCol, qty});


    }//end cookieJar inner class

Any help will be greatly appreciated, Thanks guys!

  • 1
    I know this might not be the answer you might be looking for, but can you have an expiry date on the cookie? It`s just a suggestion Jan 1, 2015 at 20:44
  • Hi @MihaiNeagoe, thanks for your reply. Expiry date, yes I've set the date to 0, but it won't work as I'm using a custom apex cookie class to store the value here: developer.force.com/cookbook/recipe/…
    – taradyn
    Jan 6, 2015 at 14:45

2 Answers 2


You just have to set the maxAge value to zero, which is the equivalent of the standard way of setting the expiry date on the past.

From the documentation: Cookie Class

MaxAge: A number representing how long a cookie is valid for in seconds. If set to less than zero, a session cookie is issued. If set to zero, the cookie is deleted.

E.g. based on cookie set in cookieJar

Cookie pId = new Cookie('productid', '',null,0,false); // Note the 0 to delete the cookie
ApexPages.currentPage().setCookies(new Cookie[]{pId});
  • Hi @pepeFloyd, thanks for your reply. Yes I've read the documentation, but am not sure on how do I remove the data based on the id found within the appended cookie.
    – taradyn
    Jan 4, 2015 at 18:38
  • 1
    @taradyn CookieJar appears to be a custom Apex Class. The Cookie id will be defined there. Jan 5, 2015 at 0:59
  • HI @DanielBallinger , thanks for your reply. I'm kinda confused. What do you mean by the cookie id will be defined there? Yes, I'm using the exact custom apex class. My cookiejar constructor contained the product id. As you can see my cookie data is appended. I tried to remove the the cookie based on the specific product id but failed.
    – taradyn
    Jan 6, 2015 at 14:42
  • @taradyn The first parameter passed to the Cookie constructor in CookieJar with be the Cookie Id you need. If that doesn't make sense, can you add your CookieJar implementation to the question. Jan 6, 2015 at 20:17
  • Hi, @DanielBallinger I've added the cookie Jar class. The first parameter in the constructor is my product id.
    – taradyn
    Jan 7, 2015 at 8:08

Instead of setting maxAge -1, set it to 0. It will delete the cookie.

You can use the Apex Cookie class.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .