Product Delete Observer

Product Delete Observer

August 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Product Delete Observer

Handling Product Deletion with Laravel Observers

As a senior developer specializing in e-commerce platforms, I often encounter situations where clients need to perform additional actions when a product is deleted from their system. In this post, we’ll explore how to create an observer for the Product model in Magento 2 using the catalog_controller_product_delete event.

Understanding Event Observers in Magento 2

Magento 2 provides an excellent event-driven architecture that allows us to tap into specific actions and perform custom functionality. The catalog_controller_product_delete event is triggered when a product is deleted from the admin interface.

However, as you’ve experienced, simply having this event does not automatically execute your desired code. You need to create an observer for this event and define what action should be performed during the deletion process.

Creating Your Observer

To start, navigate to the app/code/Vendor/Module/etc/events.xml file in your Magento 2 installation. Here, you’ll find all the registered events. Find the catalog_controller_product_delete event and copy its XML structure:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_controller_product_delete">
        <observer name="vendor_module_observer_product_delete" instance="Vendor\Module\Observer\ProductDeleteObserver"/>
    </event>
</config>

Next, create the observer class Vendor\Module\Observer\ProductDeleteObserver.php:

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Vendor\Module\Model\ApiClient; // Path to your API client model

class ProductDeleteObserver implements ObserverInterface
{
    protected $apiClient;

    public function __construct(ApiClient $apiClient)
    {
        $this->apiClient = $apiClient;
    }

    public function execute(Observer $observer)
    {
        $productId = $observer->getEvent()->getProduct()->getEntityId();

        // Your custom logic here
        $this->apiClient->sendProductDelReq($productId); // Assuming this is your method to call the API

        // If you want to stop the deletion process, return false:
        // return false;
    }
}

In this example, we’re injecting an instance of our ApiClient model. When the execute() method is called (which happens when the event triggers), it retrieves the deleted product’s ID and passes it to the sendProductDelReq() method.

Testing Your Observer

To verify that your observer is working correctly, add a die() statement inside the sendProductDelReq() method:

public function sendProductDelReq($productId)
{
    die('Product deleted: ' . $productId);
}

When you delete a product from the admin interface, you should see this message appear in your browser console or error logs.

Conclusion

In this post, we’ve demonstrated how to create an observer for the catalog_controller_product_delete event in Magento 2. By following these steps, you can add custom functionality during the product deletion process, such as sending a request to delete the same product from another system.

Remember that observers provide a powerful way to extend Magento’s core functionality without modifying the original codebase. They are an excellent tool for maintaining a clean and maintainable code structure.

For more information on Magento 2 observers and their best practices, I recommend checking out Laravel Company’s guide to Magento development.

Magento 2 Catalog Extensions Admin

Our network