You can perform event action to successfully Product deleted in Magento 2 by the catalog event name catalog_product_delete_after_done. This is the perfect event for product delete observer.
If Magento is sync with a third party ERP system and wants to sync product data between two platforms, if product deleted in Magento and want to delete a product from the ERP, You can perform this action using Magento native Event catalog_product_delete_after_done.
You can see the native Magento event declaration from the Model file, Magento/Catalog/Model/ResourceModel/Product with method name delete($object).
Create an Event in adminhtml area by events.xml file,
app/code/Jesadiya/Erp/etc/adminhtml/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="catalog_product_delete_after_done"> <observer name="custom_erp_delete_product" instance="Jesadiya\Erp\Observer\ProcessProductAfterDeleteEventObserver" /> </event> </config>
Create an Observer file,
app/code/Jesadiya/Erp/Observer\ProcessProductAfterDeleteEventObserver.php
<?php namespace Jesadiya\Erp\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class ProcessProductAfterDeleteEventObserver implements ObserverInterface { /** * Call an API to product delete from ERP * after delete product from Magento * * @param Observer $observer * @return $this */ public function execute(Observer $observer) { $eventProduct = $observer->getEvent()->getProduct(); $productId = $eventProduct->getId(); if ($productId) { //Call API to remove from ERP } return $this; } }
In Observer file, You got the product id which was the latest product delete from the Magento.
This event is used to remove product-specific data once the product is deleted from the Magento instance.
You can explore the real-time example from the Review Module with admin events in native Magento.
You can also interested in seeing A Complete List of all events in Magento 2.