Hello Magento Friends,
Welcome to Magento “How To” Blog series. Today let’s learn about How to Remove/Delete Product programmatically in Magento 2. Previously I have shared How to Hide Price for Not Logged In Customers in Magento 2.
Your Magento store is up and running but there is always a need to make some changes with products. Due to any reason you don’t need some products in your store and need to be removed from your products list. So here I am sharing How to Remove/Delete Product programmatically in Magento 2.
Steps to Remove/Delete Product programmatically in Magento 2:
Step 1: Create one file product_remove.php in the Magento root directory then add the below code.
require_once __DIR__ . '/app/bootstrap.php';
catch (\Exception $e)
echo 'Autoload error: ' . $e->getMessage();
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$appState = $objectManager->get('\Magento\Framework\App\State');
$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
$registry = $objectManager->get('\Magento\Framework\Registry');
//There are two ways to remove products using SKU or product id.
// using sku to remove product
$sky="your sku here";
//using product id to remove product
$product_id = 1; //here your product id
$product = $productRepository->getById($product_id);
echo $sky." Your Product Remove Successfully.";
Step 2: After adding the above code run like
Accordingly, you can Remove/Delete Product programmatically in Magento 2 and keep your products catalog up-to-date. If you face difficulty while implementing the above steps, mention it in the comment section below. Help your fellow Magento friends by sharing the article with them.