Hello Developers,
Here you can find quick code for the Delete Customer in Magento 2 using any custom PHP Script. Here I am giving you an example to delete customer which Id is ’45’. You can create this script in Magento root.
use Magento\Framework\App\Bootstrap;
require __DIR__ . ‘/app/bootstrap.php’;
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$appState = $obj->get(“Magento\Framework\App\State”);
$appState->setAreaCode(“admin”);
$resource = $obj->get(‘Magento\Framework\App\ResourceConnection’);
$connection = $resource->getConnection();
$obj->get(‘Magento\Framework\Registry’)->register(‘isSecureArea’, true);
$customerId = ’45’;
$customer = $obj->create(‘Magento\Customer\Model\ResourceModel\CustomerRepository’)->deleteById($customerId);
try {
echo ‘Deleted Customer for Id : ‘ . $customerId . ‘ ‘;
} catch (Exception $e){
echo ‘Error for Id : ‘ . $customerId . ‘ ‘ . $e->getMessage() . ‘‘;
}
Thank you and Keep Coding!
Leave a Reply