2

Is there a short and best way to delete bunch of products in Magento2.

I am having list if 1,20,000 SKUs and want to delete that.

Tried:

  1. Read CSV
  2. Delete products in loop by repository.

Problem:

But the problem is that it is taking too much time.

Any suggestions ?

  • put that file on the root of Magento and run that file by CMD php delbycsv.php – Ketan Borada Dec 9 '19 at 5:53
  • delbycsv.php : contains the delete code as i mentioned above right ? – Narendra Vyas Dec 9 '19 at 5:54
  • doing like this only but in a day it is deleting 10000 only – Narendra Vyas Dec 9 '19 at 5:54
  • are these selective SKUs or complete catalog ? – Naveed Asim Dec 9 '19 at 6:06
  • these are selective SKUs provided by client in a CSV file – Narendra Vyas Dec 9 '19 at 6:24
0

create below query by php code and Run it in MySQL directly.

DELETE FROM catalog_product_entity WHERE sku in (sku1, sku2, sku3, sku4, ...);

Take Backup first Database

  • But the moto of my question was that is this the best approach. Does it clear all the links from other EAV tables as well ?? What is the recommendation by Magento on the same. Did you find anywhere this is mentioned in Magento itself ? – Narendra Vyas Dec 9 '19 at 7:27
  • yes due to foreign key all related data will be deleted – Ketan Borada Dec 9 '19 at 7:34
0

Delete Products from Magento Import/Export option.

System -> Import -> select Entity Type - "Products" -> select Import Behavior - "Delete"

CSV file should contain one column with "sku". Upload CSV file and choose the delete option from Import Behavior.

0

Please try to this code. or share me your existing code to deleting product.

<?php
$objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()->addAttributeToSelect('*')->load();
$app_state = $objectManager->get('\Magento\Framework\App\State');
//$app_state->setAreaCode('frontend');

foreach ($collection as $product){
    try {
        echo 'Deleted '.$product->getName()."<br>";
        $product->delete();

    } catch (Exception $e) {
        echo 'Unable to delete product '.$product->getName()."<br>";
        echo $e->getMessage() . "<br>";
    }   
}
?>

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.