How to Remove Customizable Options of All Products Programmatically in Magento 2

How-to-Remove-Customizable-Options-of-All-Products-Programmatically-in-Magento-2

Hello Magento Friends,

Today’s learning guide is about How to Remove Customizable Options of All Products Programmatically in Magento 2.

Magento 2 catalog maintenance requires various manipulations from the store owner’s side. To increase the user experience you add a number of custom options for products. There comes a point where you need to delete all the custom options applied on Magento 2 products.

What if you have a huge number of products? Removing customizable options for each product is difficult. Hence, with the help of the below code, you can Remove Customizable Options of All Products Programmatically in Magento 2.

Let’s begin,

Steps to Remove Customizable Options of All Products Programmatically in Magento 2:

Step 1: Create remove_product_option.php in the Magento root directory and 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('global');
    $product_colletion = $objectManager->get('\Magento\Catalog\Model\Product')->getCollection();
    $product_count = 0 ;
    echo 'All Product Custom Option Remove Starting !! <br/>';
    
    foreach ($product_colletion as $product)
    {
        $product = $objectManager->get('\Magento\Catalog\Model\ProductFactory')->create()->load($product->getId());
        if ($product->getHasOptions())
        {
            $option_temp = [];
            $product->setOptions($option_temp);
            $product->setHasOptions(0)->save();
            echo "Update Your Product Name : ".$product->getName()." And Product ID :".$product->getId()." <br/>";
            $product_count++;
        }
    }
    echo 'Product Custom Option Remove Successfully.';
}
catch(\Exception $e)
{
	print_r($e->getMessage());
}

After adding the above code, run the below URL in the browser.

https://yourdomain/remove_product_option.php

Conclusion:

Hence, this was about How to Remove Customizable Options of All Products Programmatically in Magento 2. For any doubts or difficulties, mention in the comment part without any hesitation. Spread the article amongst your friends and colleagues.

Happy Coding!

Previous Article

Leading Bulk SMS Service Providers in India: Top 10 Picks

Next Article

How to Add Top Link in Magento 2

Write a Comment
  1. This worked perfectly. I had to do one minor change for my M2 configuration to the line:
    require_once __DIR__ . ‘/app/bootstrap.php’;

    The script tries to call for a file that is in the /app directory instead of /pub. However, my Magento will always resolve the /pub directory since this is how the Magento 2 app works.

    So I changed the script to:
    require_once(dirname(__FILE__).’/../app/bootstrap.php’)

    Fixed!

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨