How to disable an event observer defined by default in Magento2

If we take a look at:

vendor/magento/framework/Event/Invoker/InvokerDefault.php

 public function dispatch(array $configuration, Observer $observer)
    {
        /** Check whether event observer is disabled */
        if (isset($configuration['disabled']) && true === $configuration['disabled']) {
            return;
        }

        if (isset($configuration['shared']) && false === $configuration['shared']) {
            $object = $this->_observerFactory->create($configuration['instance']);
        } else {
            $object = $this->_observerFactory->get($configuration['instance']);
        }
        $this->_callObserverMethod($object, $observer);
    }

We can see how to share and disable a Event Observer. So, in our custom module, we can disable the Event Observer Magento default.

For example, we're going to disable wishlist_add_product event in report module vendor/magento/module-reports/etc/frontend/events.xml

Our Vendor/Module/etc/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="wishlist_add_product">
        <observer name="reports" disabled="true"/>
    </event>
</config>

The event name and observer name are the same Magento default.


Did some digging and seems its actually pretty easy to disable a certain event observer in Magento2.

All we have to do, is create a custom module and add a events.xml file to the same area where the event observer is attached, and add the following line:

<observer name="[observer _name]" disabled="true"/>

So in my case the event observer xml was at vendor/magento/module-bundle/etc/frontend/events.xml, so this means the area was frontend.

<?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="catalog_product_upsell">
        <observer name="bundle_observer" instance="Magento\Bundle\Observer\AppendUpsellProductsObserver"/>
    </event>
    ......
</config>

Thats means in our custom module we create a events.xml at lets say app/code/Foo/Bar/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">
    <!-- Disabling Default Observer Magento\Bundle\Observer\AppendUpsellProductsObserver -->
    <event name="catalog_product_upsell">
        <observer name="bundle_observer" disabled="true"/>
    </event>
</config>

Note: Here the observer name and the event name should be the same as used in default magento

That about does the trick.