How to create an custom event and observer for that event at backend in Magento2. For example I want to create an event that whenever an admin deletes a customer profile a separate log file is generated. Please help me with the detailed solution and the directory structure.

  • There is no option to delete the order in magento 2.....You can create event as order_cancel_after for cancel the order. – Ranganathan Jul 13 '19 at 11:43
  • Ok thank you I have updated my question – Naiwrita09 Jul 13 '19 at 11:46

You can achieve this using plugin instead of events

Create di.xml in app/code/Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Model\ResourceModel\CustomerRepository">
        <plugin name="customer_delete_around" type="Vendor\Module\Plugin\aroundCustomerDelete" />

And create a plugin with below code in Vendor\Module\Plugin\aroundCustomerDelete.php


namespace Vendor\Module\Plugin;

class aroundCustomerDelete
    public function aroundDeleteById(
        \Magento\Customer\Model\ResourceModel\CustomerRepository $subject,
        \Closure $proceed,
    ) {
        // This will proceed delete action
        $result = $proceed($customerId);

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->info('Your text message');

        return $result;

Or you can create a custom log file using This link

Or use customer_delete_before this event to run your script in observer

| improve this answer | |
  • I have already executed it using plugin I want to know if its possible using event and observer – Naiwrita09 Jul 13 '19 at 11:56
  • I have updated my answer....if you need demo let me know – Ranganathan Jul 13 '19 at 11:57

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