Magento – Remove the attribute programmatically in magento2

magento2.3php-7.2

I want delete the existing attribute called Manufacturer. Check if this attribute exists before deleting.

How to do this.

Thanks.

Best Answer

Try this,

In order to check and remove attribute

if(!$setup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, 'attribute_code')) {
       $eav_setup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'attribute_code');                                                        
}

Complete code on InstallData.php

<?php                                                         
namespace Vendor\Module\Setup;                                      
use Magento\Eav\Setup\EavSetupFactory;                              
use Magento\Framework\Setup\InstallDataInterface;                   
use Magento\Framework\Setup\ModuleContextInterface;                 
use Magento\Framework\Setup\ModuleDataSetupInterface;             
class InstallData implements InstallDataInterface{
 private $eav_setup_factory;

public function __construct(EavSetupFactory $eav_setup_factory)
{
    $this->eav_setup_factory = $eav_setup_factory;
}

public function install(
    ModuleDataSetupInterface $setup,
    ModuleContextInterface $context
) {
    $eav_setup = $this->eav_setup_factory->create(['setup' => $setup]);
    if(!$setup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, 'attribute_code')) {
         $eav_setup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'attribute_code');
    }
    $eav_setup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'attribute_code',
        [
            'type'          => 'varchar',
            'frontend'      => '',
            'label'         => 'Allowed Customer Group',
            'input'         => 'multiselect',
            'class'         => '',
            'attribute_set' => 'Default',
            'global'        => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'visible'       => true,
            'required'      => false,
            'user_defined'  => true,
            'default'       => null,
            'searchable'    => false,
            'filterable'    => false,
            'comparable'    => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique'        => false,
            'apply_to'      => '',
            'system'        => 1,
            'group'         => 'Attributes List'
        ]
    );
    //add product attribute to existing attribute_sets
    $entity_type_id = $eav_setup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
    $attribute_set_ids = $eav_setup->getAllAttributeSetIds($entity_type_id);
    foreach ($attribute_set_ids as $attribute_set_id) {
        $group_id = $eav_setup->getAttributeGroupId($entity_type_id, $attribute_set_id, "group_name");
        $eav_setup->addAttributeToGroup(
            $entity_type_id,
            $attribute_set_id,
            $group_id,
            'attribute_name',
            20
        );
    }
}}

NOTE : If you have module exist already do the same via UpgradeData.php

Hope this helps :)

Related Topic