---
title: "Create, Update and Remove Custom Category Attributes Using UpgradeData.php In Magento 2"
url: "https://dolphinwebsolution.com/create-update-and-remove-custom-category-in-magento-2"
date: "2021-07-25T13:30:59+05:30"
modified: "2023-08-25T09:54:56+05:30"
author:
  name: "Mahesh Makwana"
categories:
  - "Magento"
word_count: 466
reading_time: "3 min read"
summary: "We learn how to create, update and remove custom category attributes using UpgradeData.php in Magento 2. When we develop modules or themes we need some modification in custom category attributes th..."
description: "We learn how to create, update and remove custom category attributes using UpgradeData.php in Magento 2. When we develop modules or themes we need some modif..."
keywords: "Update and Remove Custom Category,Remove Custom Category,custom Custom Category, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to set logo in system configuration tab in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-set-logo-in-system-configuration-tab-in-magento-2"
  - title: "How Magento 2 identify module"
    url: "https://dolphinwebsolution.com/how-magento-2-identify-module"
  - title: "How to Create Custom Event in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-create-custom-event-in-magento-2"
---

# Create, Update and Remove Custom Category Attributes Using UpgradeData.php In Magento 2

_Published: July 25, 2021_  
_Author: Mahesh Makwana_  

![](https://dolphinwebsolution.com/wp-content/uploads/2023/02/blog-image-1-1024x614.jpg)

We learn how to create, update and remove custom category attributes using UpgradeData.php in Magento 2. **When we develop modules or themes we need some modification in custom category attributes then we use UpgradeData.php**. Magento 2 allow to create, update and remove custom category attribute.

We used the **UpgradeData.php** file for creating, updating and removing custom category attributes. UpgradeData.php file places at your module path app/code/VendoreName/ModuleName/Setup/UpgradeData.php. **This file is executed only when the module setup\_version version is updated.** Like InstallData.php file UpgradeData.php file also executes only once.

If you don’t know about UpgradeData.php then [click here](https://dolphinwebsolution.com/how-to-create-install-upgrade-uninstall-script-in-magento-2)

- **Step-1:** Update **module.xml** at **app/code/Dolphin/CategoryAttribute/etc**

```
<?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="Dolphin_CategoryAttribute" setup_version="1.0.2">
        <sequence>
            <module name="Magento_Catalog"/>
            <module name="Magento_Backend"/>
        </sequence>
    </module>
</config>
```

Here we update **setup\_version** only. You can also update the sequence if required.

- **Step-2:** Create **UpgradeData.php** at **app/code/Dolphin/CategoryAttribute/Setup**

```
<?php

namespace Dolphin\CategoryAttribute\Setup;

use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if ($context->getVersion() && version_compare($context->getVersion(), '1.0.2') < 0) {

            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

            /* For Remove Attribute */
            $eavSetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'ATTRIBUTE_CODE');

            /* For Create New Attribute */
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Category::ENTITY,
                'ATTRIBUTE_CODE',
                [
                    'type' => 'varchar',
                    'label' => 'Updated Attribute Name',
                    'input' => 'text',
                    'required' => false,
                    'sort_order' => 35,
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                    'wysiwyg_enabled' => true,
                    'group' => 'General Information',
                ]
            );

            /* For Update Attribute */
            $eavSetup->updateAttribute(\Magento\Catalog\Model\Category::ENTITY, 'ATTRIBUTE_CODE', 'position', 100);
        }
        $setup->endSetup();
    }
}
```

Here we must add the same version in both files. **If the version name does not match then the [UpgradeData.php](https://magento.stackexchange.com/questions/230805/magento-2-upgradedata-php-not-working) file not executes.** please check before updating the module.

### So after you create and update the above files Please run Magento commands
```
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
```

Please check your [app/code/Dolphin/CategoryAttribute/view/adminhtml/ui\_component/category\_form.xml](https://dolphinwebsolution.com/how-to-create-custom-category-attribute-in-magento-2) file after updating the module. This file is responsible for show attributes in category form.

I hope this helps you. For any doubts regarding this topic, please write your doubts in the comments section.


---

_View the original post at: [https://dolphinwebsolution.com/create-update-and-remove-custom-category-in-magento-2](https://dolphinwebsolution.com/create-update-and-remove-custom-category-in-magento-2)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 06:54:03 UTC_  
