I am trying to update a product with some custom attributes. I need to unset data that is being passed to set the value to none. I am currently on Magento 2.1


I added a break-point after unset the data, customAttributeA row is indeed gone from the product data array. However, after calling the save function, customAttributeA comes back into the array, with the value previously set.

Is there a way to persist the unsetData during the save function? I have also tried

$product->setData($customAttributeA , "");
$product->setCustomAttribute($customAttributeA , "");

However, none of them seems to work.

The attribute type i am trying to unset is a dropdown type.


edit- this is a snapshot of the code i am using to set/unset the data

 foreach ($listTypes as $key => $value) {
    if (isset($params[$value])){
       if(isset($params[$value . '_mid'])) {
           $product->setCustomAttribute($value, $params[$value.'_mid']);
           // unset data here

  • Any ideas anyone? I'm wasting a lot of time trying various methods but nothing seems to work. – Derek Dec 16 '16 at 4:01
  • just to update, whoever comes across this issue in the future. I've given up doing this via the product model, i am directly querying the database tables to remove the option-value row directly. – Derek Dec 16 '16 at 9:30

I found this one https://stackoverflow.com/a/8672847. It looks like Magento 1 code but it's work for me.

$resource = $product->getResource();

You have to try with below for unset data,

1st way

You have to use setData() instead of unsetData() for unset data.


2nd Way,

  • Thanks for the reply Rakesh. i tried the 1st way, it does have the same behavior as the methods i used previously. It sets the attribute value to null, however, it doesn't persist through the save method. And the second method, doesn't seem to affect anything though ( I used $updatedProduct->unset($customAttributeA); – Derek Dec 13 '16 at 5:58
  • I just understood your 2nd method, i'll try it again soon – Derek Dec 13 '16 at 7:50
  • Is it useful in your case? – Rakesh Jesadiya Dec 13 '16 at 8:33
  • I just tried, nope, method 2 does not work as well unfortunately. – Derek Dec 13 '16 at 8:34

I could not find a way to unset a custom attribute on a product, so I removed the record from the database instead. Below is the code if anyone is interested.

private function unsetCustomAttribute($magentoProduct, $attributeCode)
    $this->logger->debug("Unset custom attribute '" . $attributeCode . "' on product '" . $magentoProduct->sku . "'");

    /** @var ProductAttributeRepositoryInterface $attributeRepository */
    $attributeRepository = ObjectManager::getInstance()->get(ProductAttributeRepositoryInterface::class);
    $attribute = $attributeRepository->get($attributeCode);

    /** @var ResourceConnection $resources */
    $resources = ObjectManager::getInstance()->get(ResourceConnection::class);

    /** @var AdapterInterface $connection */
    $connection = $resources->getConnection();
    $connection->delete("catalog_product_entity_int", "entity_id = " . $magentoProduct->id . " AND attribute_id = " . $attribute->getAttributeId());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.