0

I try to delete product but it returns exception can not delete product and sku my code is here

 $productcollection = $this->collectionFactory->create()->addAttributeToSelect('manufacturer','LG')->load();

        foreach ($productcollection as $product) {
            try {

                $this->productRepository->delete($product);
            } catch (StateException $e) {
            }
        }

how can I solve this error ?

1

If you try to delete product from frontend then you need to assign area for that.

Add following code to your class.

public function __construct(
    ........
    \Magento\Catalog\Model\ProductRepository $productRepository,
    \Magento\Framework\Registry $registry
) {
    ......
    $this->productRepository = $productRepository;
    $this->registry = $registry;
}

Following code is for deleting product.

$this->registry->register('isSecureArea', true);
// using sku
$this->productRepository->deleteById('Z62676');

// using product id
$product = $this->productRepository->getById(1);
$this->productRepository->delete($product);

Delete All Products Example using object manager :

$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>";
    }   
} 
  • Unable to apply data patch DevAll\ExtensionSettingConfig\Setup\Patch\Data\RemoveLGManufacturer for module DevAll_ExtensionSettingConfig. Original exception message: Registry key "isSecureArea" already exists. I think using Registry is deprecated in Magento 2.3.3 . Thanks for answer . – Giga Todadze Mar 25 at 5:03
  • try without registry it may work. – Vikas kalal Mar 25 at 5:17
  • unfortunately it doesn't. – Giga Todadze Mar 25 at 6:00
1

Do some corrections in the code. Hopefully, it will work for you.

$productcollection = $this->collectionFactory->create()->addAttributeToSelect('manufacturer','LG')->load();
    foreach ($productcollection as $product) {
        try {
            $product->delete();
        } catch (StateException $e) {
        }
    }

This code will gives you result what you want.

  • you just changed ``` $this->productRepository->delete($product);``` by $product->delete(); but it doesn't work=> Original exception message: Area code is not set – Giga Todadze Mar 25 at 5:59
  • Where you are running this code? In the root PHP script? If yes, then you need to add frontend areacode above the code like this: $state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); – Kartik Asodariya Mar 25 at 6:18
  • its datapatch . – Giga Todadze Mar 25 at 6:29
  • Yes i needed to add area code on frontend . – Giga Todadze Mar 25 at 8:53
0
$productcollection = $this->collectionFactory->create()->addAttributeToSelect('manufacturer','LG')->load();
foreach ($productcollection as $product) {
    try {
        $productcollection->removeItemByKey($product->getId());
    } catch (StateException $e) {
    }
}
return $productcollection;
      }

Thankful of my friend Sandro who helped me with the solution.

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.