3

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 ?

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

4 Answers 4

2

It's Magento default provide functionality using csv import.

Go to Admin >> System >> Data Trasfer >> Import. https://prnt.sc/26ct8w6

CSV file :

enter image description here

Admin setting: enter image description here

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

2
  • 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 ? Dec 9, 2019 at 7:27
  • yes due to foreign key all related data will be deleted Dec 9, 2019 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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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