Deleting Attribute Through Patch ( Programmatically ) In Magento
Method To Assigning A Group To The Product Attribute.
1- For deleting product attributes you need to create custom modules.
Like:
Vendor/Module
Esparks/DeleteAttr
2- Create DeleteCustomAttributes.php file inside.
app/code/Esparks/DeleteAttr/Setup/Patch/Data
And paste this code into it:
<?php
// Created By @Esparks
namespace Esparks\DeleteAttr\Setup\Patch\Data;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Catalog\Model\Product;
class DeleteCustomAttributes implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var AttributeRepositoryInterface
*/
protected $attributeRepository;
/**
* DeleteCustomAttributes constructor.
* @param ModuleDataSetupInterface $moduleDataSetup
* @param AttributeRepositoryInterface $attributeRepository
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
AttributeRepositoryInterface $attributeRepository
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->attributeRepository = $attributeRepository;
}
/**
* @return DataPatchInterface|void
*/
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
foreach ($this->getAttributesToDelete() as $attributeCode) {
try {
$attributeData = $this->attributeRepository->get(
Product::ENTITY,
$attributeCode
);
$this->attributeRepository->deleteById(
$attributeData->getAttributeId()
);
// goto catch when attribute does not exist
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
var_dump($e->getMessage());
// // goto catch of type StateException when getting error on Delete
} catch (\Magento\Framework\Exception\StateException $e) {
var_dump($e->getMessage());
} catch (\Exception $e) {
var_dump($e->getMessage());
}
}
$this->moduleDataSetup->getConnection()->endSetup();
}
/**
* @return array
*/
protected function getAttributesToDelete()
{
return [
// paste the Attribute code here you want to delete
'customs_attributes'
];
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
}
getDependencies()’ method specifies the class ‘CreateCustomAttr’ as the dependency. It means that the ‘CreateCustomAttr’ data patch will be executed before this data patch.
In summary, this code is a Magento data patch that deletes a specific attribute from the product entity in Magento. You can customize the attribute code to be deleted by modifying the ‘getAttributesToDelete()’ method.
Finally:
Run these commands:
php bin/magento setup:upgrade
Additional Information:
To find the attribute you want to delete in Magento 2, you can follow these steps:
- 1.Log into your Magento 2 admin panel.
- 2. Navigate to Stores > Attributes > Product.
- 3. You will see a list of all product attributes.
- 4. Locate the attribute you want to delete from the list.
You can use the search or filter options to narrow the list if needed.
Once you find the attribute you want to delete, please take note of its attribute code. The attribute code is a unique identifier for the attribute.
Caution:
If you add the “ ‘is_user_defined’ => true ” property then you can delete the programmatically created attribute from admin, else you can’t delete the attribute From admin which is created by patch (programmatically).