Skip to main content

How to delete color attribute option by using label in Magento 2?

Can we delete product attribute options by using label?

For example say attribute:color has options like green, red, yellow how to delete red from this? I used below code.

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $eavConfig = $objectManager->get('Magento\Eav\Model\Config');

        $attribute = $eavConfig->getAttribute('catalog_product', 'color);

        $id = $attribute->getAttributeId();

        $options = $attribute->getSource()->getAllOptions();
        foreach ($options as $option) {             
            $options['delete'][$option['value']] = true; 
            $options['value'][$option['value']] = true;
        }
         
        $eavSetup = $this->_objectManager->get('Magento\Eav\Setup\EavSetup');
        $eavSetup->addAttributeOption($options);

It is deleting all the options. I need to delete specific option.

Jafar Pinjar
  • 2k
  • 7
  • 70
  • 144