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