Back to Top

How to delete custom attribute options in Magento 2

Updated 22 February 2024

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();

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

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


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home