I want to remove all customers that have the following string in their first name: 'http://' (spam customers that have links in their first names).

Is there a good way to remove these programmatically using a setup script in Magento 2?

There must be a better way than to get the filtered customer collection and delete them one by one right?

--------- UPDATE -----------

I am currently using the following method. It works, but it takes quite some time:

 * Remove Customers with links (http:// or https://) in their names
private function removeFakeCustomers()
    $this->registry->register('isSecureArea', true);
    $customers = $this->customerFactory->create();
            ['like' => '%http://%'],
            ['like' => 'http://%'],
            ['like' => '%https://%'],
            ['like' => 'https://%']

    if (!$customers) {

    echo 'Start removing fake customers...';

    foreach ($customers as $customer) {

    echo 'Fake Customers removed...';
  • Do you want to try it from database directly? It will save time and also delete related values as foreign key is also enabled for customer tables. – Sukumar Gorai Sep 13 '18 at 12:27
  • @SukumarGorai I have added my current method to do it, but as I explained in the description, it does take quite some time. So, if deleting it from the database will save time, then yes please! – Maarten Wolfsen Sep 13 '18 at 12:31
  • DELETE FROM customer_entity WHERE firstname like '%http://%'; You can try this. This is just an example. It will not take much time. If still it takes time then you can try DELETE FROM customer_entity WHERE firstname like '%http://%' limit 5000; – Sukumar Gorai Sep 13 '18 at 12:34
  • @SukumarGorai but (correct me if I am wrong), there are more tables than just customer_entity that store customer data right? – Maarten Wolfsen Sep 13 '18 at 12:36
  • Yes. You are right. But magento relates the other tables with foreign key and it should delete the other values which is related to customers but one thing which will not work in both the cases(ours and yours) it will not delete the address data. – Sukumar Gorai Sep 13 '18 at 12:42

create one script file test.php in magento root folder.

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');

    $OM = \Magento\Framework\App\ObjectManager::getInstance();
    $collectionobj = $OM->create('\Magento\Customer\Model\Customer');
    $customercollection = $collectionobj->getCollection()
            ->addAttributeToFilter("firstname", array("like" => "%http%"));
    foreach($customercollection as $customer)

catch(\Exception $e)
    echo $e->getMessage();

Run these file from browser like www.domain.com/test.php

  • I don't really want to use a 'standalone' script for this, I'd rather use Magento's setup system (UpgradeData). Plus, I don't think adding such a script in the root is a safe thing to do, even if it's just for the duration of the script. – Maarten Wolfsen Sep 13 '18 at 12:38
  • you can use same code into your UpgradeData as well. You need to just take code which write in try block. – Dhiren Vasoya Sep 13 '18 at 12:47
  • I am already using that code, but I am wondering if there is a faster solution. This way works quite well, but is quite slow – Maarten Wolfsen Sep 13 '18 at 12:53
  • Alternet solution is that, you can use sql query to remove customer entity. That become faster. – Dhiren Vasoya Sep 13 '18 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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