How to delete custom attribute options in Magento 2
Here we will learn, how to delete custom attribute options programmatically in magento 2.
Use following code to remove the attribute options in Magento2.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$attributeId = 487; // Id of attribute
$attributeModel = $objectManager->create('Magento\Customer\Model\Attribute')->load($attributeId);
$data = [];
$options = $attributeModel->getSource()->getAllOptions(); //get all options
foreach ($options as $value) {
if (is_numeric($value['value'])) {
$data['option']['delete'][$value['value']] = 1;
$data['option']['value'][$value['value']] = $value['label'];
}
}
$attributeModel->addData($data);
$attributeModel->save();
View Comments
Comment or Ask a Question
Quick Links