In this blog, we will find out how to delete product attributes programmatically in Magento 2 using the InstallData.php file. Using the way of deleting a product attribute programmatically saves lots of time instead of manually.
Steps to Remove Product Attribute Programmatically in Magento 2:
To remove a product attribute, Firstly we need to create a simple module for that.
Create the first registration.php, composer.json, and module.xml file for defining our module. Here I have used Webiators as Vendor/Namespace where DeleteProductAttribute is a module name.
Create registration.php file
Path: app/code/Webiators /DeleteProductAttribute /registration.php
1 2 3 4 5 6 |
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Webiators_DeleteProductAttribute', __DIR__); |
Create composer.json file
Path: app/code/Webiators /DeleteProductAttribute/composer.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
{ "name": "webiators/module-deleteproductattribute", "description": "DeleteProductAttribute module by Webiators", "require": {}, "type": "magento2-module", "version": "1.0.3", "license": [ "OSL-3.0", "AFL-3.0" ], "autoload": { "files": [ "registration.php" ], "psr-4": { "Webiators\DeleteProductAttribute": "" } } } |
Create module.xml file
Path: app/code/Webiators/DeleteProductAttribute/etc/module.xml
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Webiators_DeleteProductAttribute" setup_version="1.0.0"> <sequence> <module name="Magento_Catalog"/> </sequence> </module> </config> |
Create an InstallData.php file to install our custom product attribute in the Magento instance.
Path: app/code/Webiators/DeleteProductAttribute/Setup/InstallData.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php namespace Webiators\DeleteProductAttribute\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\Catalog\Model\Product::ENTITY, 'Test_attribute'); } } |
Using Command-line, Go to Magento instance where you have installed Magento, Open Command Line
1 2 3 4 5 6 7 |
php -dmemory_limit=5G bin/magento setup:upgrade php -dmemory_limit=5G bin/magento setup:di:compile php -dmemory_limit=5G bin/magento setup:static-content:deploy -f en_US php -dmemory_limit=5G bin/magento cache:flush php -dmemory_limit=5G bin/magento cache:clean php -dmemory_limit=5G bin/magento indexer:reindex |
Now it’s time to see the result:
Go to Admin Dashboard > Stores > Attributes > Product and check if an attribute exists or not.
Read More