Delete Product from Magento 2 Programatically

<?php
   use \Magento\Framework\App\Bootstrap;

   include('../app/bootstrap.php');

   $bootstrap = Bootstrap::create(BP, $_SERVER);
   $objectManager = $bootstrap->getObjectManager();
   $url = \Magento\Framework\App\ObjectManager::getInstance();
   $storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
   $state = $objectManager->get('\Magento\Framework\App\State');
   $state->setAreaCode('frontend');

   $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
                                               ->addAttributeToSelect('*')
                                    ->addFieldToFilter('entity_id',array('gteq'=>1))
                                    ->addFieldToFilter('entity_id',array('lteq'=>10));

   foreach ($productCollection as $product){
      $registry = $objectManager->get('\Magento\Framework\Registry');
      $registry->register('isSecureArea', true);
      $product->delete();
   }

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.