Magento 2 Remove Customer Attribute Programmatically

To remove customer attribute programatically, create InstallData.php and and define the method intsall() as follows

File : app/code/PHPCodez/First/Setup/InstallData.php

<?php
 namespace PHPCodez\First\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){
 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $eavSetup->removeAttribute(
 \Magento\Customer\Model\Customer::ENTITY,
 'phpcodez_attribute');
 }
 }

Purge cache and deploy static content by running the following commands.

php bin/magento cache:flush
php bin/magento setup:static-content:deploy -f

Leave a Reply

Your email address will not be published. Required fields are marked *