7

I am trying to use below code but it is not working,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->load($productID)->delete();
7

If you try to delete product from fronend 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);
4

First, I suggest you try to avoid using the ObjectManager directly

Second, I reckon you should use the \Magento\Catalog\Api\ProductRepositoryInterface service contrat to delete a product:

protected $_productRepositoryInterface;

public function __construct(
     ...
     \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface, 
     ...
) {
    ...
    $this->_productRepositoryInterface = $productRepositoryInterface;
    ...
}

Then in your code you can do:

$product = $this->_productRepositoryInterface->getById($productID);
$this->_productRepositoryInterface->delete($product);

Note that if you have the sku of your product you can do it in one line:

$this->_productRepositoryInterface->deleteById($productSku);
2

Indeed, you cannot delete product on the frontend area.

You need to force the SecureArea registry.

But if you check the register function, you see that you cannot override an existent key value. You need to unregister the key before register it.

/**
 * Register a new variable
 *
 * @param string $key
 * @param mixed $value
 * @param bool $graceful
 * @return void
 * @throws \RuntimeException
 */
public function register($key, $value, $graceful = false)
{
    if (isset($this->_registry[$key])) {
        if ($graceful) {
            return;
        }
        throw new \RuntimeException('Registry key "' . $key . '" already exists');
    }
    $this->_registry[$key] = $value;
}

Solution based on other posts :

Constructor :

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

Logic :

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

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

Please try the following script.

function deleteAllProducts($objectManager) {

$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().PHP_EOL;
        $product->delete();

    } catch (Exception $e) {
        echo 'Failed to remove product '.$product->getName() .PHP_EOL;
        echo $e->getMessage() . "\n" .PHP_EOL;
    }   
}      
}

Click here for detailed explanation. http://www.pearlbells.co.uk/delete-magento-2-products-programmatically/

0

Try below code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$product = $objectManager->create('Magento\Catalog\Model\Product');
$product->load($productID)->delete();
  • This solution is not working for me,This is same code as what I was using to delete the product. – Ranjeet Singh Oct 25 '16 at 7:12
  • Any errors are display ? – Suresh Chikani Oct 25 '16 at 7:14
  • Not any specific error, I found that we need to use $this->registry->register('isSecureArea', true); – Ranjeet Singh Oct 25 '16 at 7:16

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.