How To

How to Remove/Delete Product programmatically in Magento 2

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.

Introduction:

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.

<?php
use Magento\Framework\AppInterface;
try
{
    require_once __DIR__ . '/app/bootstrap.php';
}
catch (\Exception $e)
{
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}
try
{
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('\Magento\Framework\App\State');
    $appState->setAreaCode('frontend');

    $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
    $registry = $objectManager->get('\Magento\Framework\Registry');

    $registry->register('isSecureArea', true);

    //There are two ways to remove products using SKU or product id.
    // using sku to remove product
    $sky="your sku here";
    $productRepository->deleteById($sky);

    //using product id to remove product
    $product_id = 1; //here your product id
    $product = $productRepository->getById($product_id);
    $productRepository->delete($product);

    echo $sky." Your Product Remove Successfully.";
}
catch(\Exception $e)
{
    print_r($e->getMessage());
}

Step 2: After adding the above code run like

https://yourdomain/product_remove.php/

That’s it.

Conclusion:

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. 

Happy Coding!

Click to rate this post!
[Total: 9 Average: 4.3]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • Hi your code to remove products works but if you wanted to delete all products without listing product id's what would the code then be?

Recent Posts

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

8 hours ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

3 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

3 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

6 days ago