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?