I wish to delete cookies while the customer clicked the log out button?

Can you please suggest us the best way to do it in magento 2

1 Answer 1


You can use customer_logout event and clear cookies using Observer.

Create files in your modules as follows:

  1. Vendor/Module/etc/frontend/events.xml :

    <?xml version="1.0" ?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="customer_logout">
           <observer instance="Vendor\Module\Observer\Frontend\Customer\Logout" name="vendor_module_observer_frontend_customer_logout_customer_logout"/>
  2. Create Observer Vendor/Module/Observer/Frontend/Customer/Logout.php

      * Copyright ©  All rights reserved.
      * See COPYING.txt for license details.
     namespace Vendor\Module\Observer\Frontend\Customer;
     class Logout implements \Magento\Framework\Event\ObserverInterface
          * Execute observer
          * @param \Magento\Framework\Event\Observer $observer
          * @return void
         public function execute(
             \Magento\Framework\Event\Observer $observer
         ) {
             //Your code here to clear the cookies

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.