What is the correct way to completely remove category attributes from the database?
Attributes were added this way using setup script:
$setup->addAttribute('catalog_category', 'seo_text', array(
'group' => 'General',
'label' => 'Additional Text',
'type' => 'text',
'input' => 'textarea',
'backend' => '',
'frontend' => '',
'visible' => true,
'required' => false,
'searchable' => false,
'user_defined' => true,
'visible_on_front' => true,
'wysiwyg_enabled' => false,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE
));
$setup->addAttribute('catalog_category', 'cat_icon', array(
'group' => 'General',
'label' => 'Category icon',
'type' => 'varchar',
'input' => 'select',
'source' => 'categoryseo/source_ico',
'backend' => '',
'frontend' => '',
'visible' => true,
'required' => false,
'searchable' => false,
'user_defined' => true,
'visible_on_front' => true,
'wysiwyg_enabled' => false,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE
));
Later these attributes were removed in another setup script, I used the standard code which can be found in many tutorials:
$setup = $this;
$setup->startSetup();
$setup->removeAttribute('catalog_category', 'seo_text');
$setup->removeAttribute('catalog_category', 'cat_icon');
$setup->endSetup();
This deleted the attributes from my categories. I also checked in the database and attributes were also removed from the table eav_attribute
.
But data of these attributes is still stored in other tables. E.g. in table catalog_category_entity_varchar
there is still info about attribute "cat_icon" for many categories.
Shouldn't all this data be automatically removed when I delete the attributes with $setup->removeAttribute(...)
method? Does it mean that I have some errors in my Magento so Magento doesn't fully remove the attribute data?
Or did I just delete the attributes incorrectly? If yes, then how to do it properly? How to delete eav attributes and all the data associated with deleted attributes?
Best Answer
If you got orphan attribute values it means that there is something wrong with your database.
For example the table
catalog_category_int
has a constraint on theattribute_id
column.This means that if the attribute is deleted, all the values for the attribute from the table
catalog_category_int
will be deleted.It's the same for the rest of the tables (
catalog_category_*
).From experience I saw that usually constraints are lost during a migration process. You export the database but without the constraints. If this is the case then remember in the future to use
System->Tools->Backup
to create database dumps for migration.[EDIT]
Here is the code I've used. I've created a simple extension that adds an attribute to the category entity, I edited a category and filled in a value for that attribute, then deleted the attribute through an upgrade script.
Here are the extension files:
app/etc/module/Easylife_Attr.xml
- declaration fileapp/code/local/Easylife/Attr/etc/config.xml
- the configuration fileapp/code/local/Easylife/Attr/sql/easylife_attr_setup/install-0.0.1.php
- the install scriptNow add a value for this dummy attribute for a category.
You should see it in the table
catalog_category_entity_text
.After that create an upgrade script to remove the attribute.
app/code/local/Easylife/Attr/sql/easylife_attr_setup/upgrade-0.0.1-0.0.2.php
and change in
config.xml
the version from0.0.1
to0.0.2
.Refresh any page and look in the same table
catalog_category_entity_text
. The value that was added for thedummy
attribute is gone. (at least for me).