Magento 2 : Delete Customer Programmatically

logo logo

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!

Author Image

Ajay Prajapati

Ajay Prajapati is a key member of Aims Infosoft, leading Business Development and Client Engagement. He is the primary point of contact for clients and prospects, ensuring that the company’s solutions align with business needs and deliver measurable value. He represents the company in client discussions, handles pre‑sales processes, and coordinates with the technical and project management teams to deliver seamless experiences.

Related Posts

A Word From Our Proud Clients

See what our most successful clients have to say about working with us...