10

I want to remove the custom attribute created for customer using installchema,I followed this link but it didn't helped.Anyone tried earlier,please help.Thanks in advance!
I have added attribute manually using installschema script and the code is here

namespace Bibhu\Customattribute\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'sample_attribute',
            [
                'type' => 'int',
                'label' => 'Sample Attribute',
                'input' => 'select',
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'required' => true,
                'default' => '0',
                'sort_order' => 100,
                'system' => false,
                'position' => 100
            ]
        );
        $sampleAttribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'sample_attribute');
        $sampleAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
        );
        $sampleAttribute->save();

            }
}

and to remove using this Uninstall.php script

    <?php
namespace Bibhu\Customattribute\Setup ;

    class Uninstall implements \Magento\Framework\Setup\UninstallInterface
    {
        protected $eavSetupFactory;
        public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
        {
            $this->eavSetupFactory = $eavSetupFactory;
        }
        public function uninstall(\Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
        {
            $setup->startSetup();
            $eavSetup = $this->eavSetupFactory->create();
            $entityTypeId = 1; 
            $eavSetup->removeAttribute($entityTypeId, 'sample_attribute');
            $setup->endSetup();
        }
    }

4 Answers 4

16

You should use UpgradeData now.

Create this file app/code/Bibhu/Customattribute/Setup/UpgradeData.php :

namespace Bibhu\Customattribute\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

/**
 * Class UpgradeData
 * @package Bibhu\Customattribute\Setup
 */
class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * UpgradeData constructor.
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '2.0.1') < 0) {
            $this->upgradeSchema201($setup);
        }

        $setup->endSetup();
    }

    /**
     * @param ModuleDataSetupInterface $setup
     */
    private function upgradeSchema201(ModuleDataSetupInterface $setup)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->removeAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'sample_attribute'
        );
    }
}

... change your module version (make sure to increase it) in app/code/Bibhu/Customattribute/etc/module.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Bibhu_Customattribute" setup_version="2.0.1"></module>
</config>

... then update your module using command line :

php bin/magento setup:upgrade
1

I use customer setup to delete custom customer attribute in my controller, after delete the custom customer attribute i clean the cache programmatically, the whole code goes like this:

use Magento\Customer\Setup\CustomerSetupFactory; 

class Delete  extends \Magento\Backend\App\Action
{
    public function __construct(
        CustomerSetupFactory $customerSetupFactory, ,
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
    ) {
        $this->customerSetupFactory =$customerSetupFactory; 
        $this->_cacheFrontendPool = $cacheFrontendPool;
        $this->_cacheTypeList = $cacheTypeList;
        parent::__construct($context);
    }

    public function execute()
    {
     $this->attribute_code = 'custom_attribute_code';
     $this->removeEavCustomer();
    }

    private function removeEavCustomer(){
      $customerSetup = $this->customerSetupFactory->create();
      $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, $this->attribute_code);

      $this->cleanCache();
    }

    private function cleanCache(){
      $types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration','config_integration_api','full_page','translate','config_webservice');
      foreach ($types as $type) {
          $this->_cacheTypeList->cleanType($type);
      }
      foreach ($this->_cacheFrontendPool as $cacheFrontend) {
          $cacheFrontend->getBackend()->clean();
      }
    }

}
0

Try below script

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom-attribute-code');
1
  • 1
    i tried this but not working Mar 27, 2017 at 11:21
0

Okay,it may be not the right way but I am able to delete the attribute by removing the particular attribute row from eav_attribute table from my db.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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