1

How to remove customer attributes which are created during install 3rd party extention in magento 2?

3
  • You want to remove using code or directly in database. Commented Mar 30, 2019 at 10:34
  • Check this linkhttps://magento.stackexchange.com/a/209867/60921 it may be helpful Commented Mar 30, 2019 at 10:37
  • We add one extension which is we want remove fully; from files & also from database.
    – Anil
    Commented Mar 30, 2019 at 11:05

2 Answers 2

1

Try This :-

namespace Company\Modulename\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; // Attributte Id
         $eavSetup->removeAttribute($entityTypeId, 'attribute_code');
         $setup->endSetup();
    }
}
7
  • We add 'github.com/amitshree/magento2-account-approval' this extention. in this only public function install; so we create one more function in 'InstallData.php' file ?
    – Anil
    Commented Mar 30, 2019 at 10:58
  • Create script in magento2-account-approval/Setup/Uninstall.php after that follow below command: php bin/magento setup:upgrade php bin/magento setup:static-content:deploy en_US -f php bin/magento cache:clear Commented Mar 30, 2019 at 11:12
  • When we add 'Uninstall.php' at that time any necessary to remove 'InstallData.php' from 'magento2-customer-account-approval/Setup' before command run ?
    – Anil
    Commented Mar 30, 2019 at 11:49
  • Not need to removed. Commented Mar 30, 2019 at 11:50
  • Thanks Very much. its working for us. We change some other things. add this Uninstall.php file then delete attribute from database. So my work is done as per our requirement.
    – Anil
    Commented Mar 30, 2019 at 12:21
0

You can create either an upgrade or install script in your custom module. Here an example with an install script:

<?php

 namespace vendor\module\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;

 class InstallData implements InstallDataInterface
 {
 private $eavSetupFactory;

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

 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
 {

 $setup->startSetup();

 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $eavSetup->removeAttribute(
 \Magento\Customer\Model\Customer::ENTITY,
 'custom_attribute');

 $setup->endSetup();

 }
 }
4
  • Thanks for reply. We add 'github.com/amitshree/magento2-account-approval' this extention. Please guide me what to do for it.
    – Anil
    Commented Mar 30, 2019 at 10:57
  • We want to remove this extention from our site. but in customer data value of attribute which is generated by this extention. so we remove all data and attribute which is generated by this extention.
    – Anil
    Commented Mar 30, 2019 at 11:01
  • We add 'public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'approve_account'); $setup->endSetup(); }' this code as your answer update but attribute not remove.
    – Anil
    Commented Mar 30, 2019 at 11:24
  • check this magento.stackexchange.com/questions/124059/…
    – fmsthird
    Commented Mar 30, 2019 at 11:39

Your Answer

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

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