9

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();
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
Ranjeet Singh
  • 453
  • 4
  • 7
  • 17

5 Answers5

10

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);
Sohel Rana
  • 35,846
  • 3
  • 72
  • 90
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);
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
3

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);
Franck Garnier
  • 2,946
  • 1
  • 17
  • 35
1

Try below code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$product = $objectManager->create('Magento\Catalog\Model\Product');
$product->load($productID)->delete();
Suresh Chikani
  • 15,836
  • 11
  • 62
  • 99
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/

Liz Eipe C
  • 1,286
  • 12
  • 17