Delete attribute options magento 2

This tutorial explains how to delete attribute options magento 2 programmatically.The script get all the attribute options and delete the options as shown below.

Please also refer the tutorial explains how to import attribute options magento 2 programmatically and other magento tutorials.

Delete attribute options magento 2
delete-attribute-options-magento-2

Step by step explanation

  • Initialise script
  • Delete attribute options magento

Delete attribute options magento 2

The script get all the attribute options from the attribute ‘color’ and delete programmatically.

<?php
use MagentoFrameworkAppBootstrap;
include('../app/bootstrap.php');

// add bootstrap
$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();

$app_state = $object_Manager->get('MagentoFrameworkAppState');
$app_state->setAreaCode('frontend');

$eavConfig = $object_Manager->get('MagentoEavModelConfig');
$attribute = $eavConfig->getAttribute('catalog_product', 'color');
$id = $attribute->getAttributeId();
$options = $attribute->getSource()->getAllOptions();

foreach ($options as $option) {
    echo 'Delete label : '.$option['label'].PHP_EOL;  
    $options['delete'][$option['value']] = true; 
    $options['value'][$option['value']] = true;
}

$eavSetup = $object_Manager->get('MagentoEavSetupEavSetup');
$eavSetup->addAttributeOption($options);

Please also refer the tutorial explains how to import attribute options magento 2 programmatically and other magento tutorials.

Share this Tutorial

Leave a Reply

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

*