0

I've been checking different threads about how to delete a product programmatically in Magento 2. The different solutions proposed are:

  • Using the Registry to set after that the securedArea parameter
  • Overriding type RemoveAction arguments in our di.xml
  • Using Magento\Catalog\Api\ProductRepositoryInterface

This solutions are discussed in this thread: Delete operation is forbidden for current area

The only one that works fine is setting the isSecuredArea parameter to true. But in Magento 2.4, the use of the class Magento\Framework\Registry is deprecated.

What is the correct way to delete a product programmatically in Magento 2.4?

Thanks!

2
  • Did you manage to find an answer to this question? I'm in the same predicament. – gabtzi Dec 6 '20 at 10:14
  • Hi @gabtzi. Sadly, I didn't. – PauGNU Dec 7 '20 at 13:34
1
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;

class DeleteProduct
{
    /** @var ProductRepository */
    private $productRepository;

    /**
     * DeleteProduct constructor.
     * @param ProductRepositoryInterface $productRepository
     */
    public function __construct(
        ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    /**
     * @param int $productId
     * @throws NoSuchEntityException
     * @throws StateException
     */
    public function delete(int $productId): void
    {
        $this->productRepository->deleteById($productId);
    }
}

run php bin/magento setup:di:compile

so that dependency injection knows about the new class and its dependencies

http://www.rosenborgsolutions.com/magento-new-class-instance.php

if you are using command object, can you inject state in your command object?


use Magento\Framework\App\State;

/** @var State */
private $appState;

public function __construct(
        State $state,
) {
    $this->appState = $state;
}

protected function execute(InputInterface $input, OutputInterface $output): void
{
    $this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
}   

1
  • This doesn't work for me. When I try to execute it, I get the error «Delete operation is forbidden for current area». Setting the area code doesn't work either (I've tried setting the area to 'frontend' and 'adminhtml'. – PauGNU Sep 16 '20 at 6:25
0

Please try the following code.

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

Hope it works for you

2
  • Hi @sarvesh, thanks for your answer, but using the object manager is not the recommended good practice by magento. I would like to do this with DI, any idea? – PauGNU Sep 15 '20 at 12:52
  • okay, will update to you. – sarvesh Dineshkumar Patel Sep 15 '20 at 12:56

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.