2

I have thousands of spam accounts in the customer database. Is there a way to quickly remove these?

I cannot do it quickly in admin panel, as it will crash the site if I try to delete them all at once. If I only delete 200 at a time it will take a very long time.

Is there a query I can run in SQL/PhpMyAdmin? All the spam accounts have the qq.com email address.

5

Try below SQL Query:

DELETE FROM customer_entity WHERE email LIKE '%qq.com';

I hope it helps...!!!

  • Could you please help me on this question. magento.stackexchange.com/questions/293336/… – Kirti Nariya Oct 31 at 5:40
  • Hi @Balwant Singh I did run the query and it looks like it worked but I go to Magento admin Customers > All Customers and there are still about 30,000+ records. There should be 546 according to the database... will it take a while to update in Magento? – YorkieMagento Nov 7 at 17:45
0

First thing you need to do if you want to programmatically delete the customers, is identify a common pattern with the accounts (e.g emails all end in provider.ru)

Next, you will want to backup your database in case anything goes wrong. And ideally, test this on a DEV/UAT site.

Following code examples are untested but should put you in the right direction, verify them yourself on a dev/uat environment and take proper backups beforehand.


SQL

After that, you can execute a select on the customer_entity and confirm all the accounts are spam ones, then run a delete with the same conditions

SELECT * FROM customer_entity WHERE email LIKE '%provider.ru';
DELETE FROM customer_entity WHERE email LIKE '%provider.ru';

Mage 2 Script

A script version of the above would be something like the following, uploading into the root of the site and executing from cli with php tends to work best

<?php

ini_set('display_errors', 1);
ini_set('max_execution_time', 0);
ini_set("memory_limit", "-1");
set_time_limit(0);
error_reporting(E_ALL);

require './app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('admin');

$customerFactory = $objectManager->create(\Magento\Customer\Model\CustomerFactory::class);
$customers = $customerFactory->create()->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('email', array('like' => '%provider.ru'));

foreach ($customers as $customer) {
    echo "Found Customer: {$customer->getId()} :: {$customer->getEmail()}",PHP_EOL;
    // $customer->delete();
}

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.