Magento – Programmatically Delete Product Attribute Sets

attribute-setmagento2product-attribute

Due to some shoddy code from a previous employee I have inherited a Magento 2 store with thousands of erroneous product attributes and hundreds of attribute sets.

I've been able to delete the attributes and move all the products back on to the Default attribute set, but now I'm left with hundreds of empty, unused product attribute sets that I can't find how to delete easily?

Best Answer

You can remove an attribute set using Magento\Catalog\Api\AttributeSetRepositoryInterface method deleteById

namespace {NameSpace};


class Test
{
    /**
     * @var \Magento\Catalog\Api\AttributeSetRepositoryInterface
     */
    protected $attributeSetRepository;

    public function __construct(
        \Magento\Catalog\Api\AttributeSetRepositoryInterface $attributeSetRepository
    )
    {
        $this->a

ttributeSetRepository = $attributeSetRepository;
        }
        public function deleteAttributeSet()
        {
            $attributeSetId = 15;
            $this->attributeSetRepository->deleteById($attributeSetId);
        }
    }

Via Object manager:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $attributeSetRepository = $objectManager->create('\Magento\Catalog\Api\AttributeSetRepositoryInterface');
        $attributeSetRepository->deleteById($attributeSetId);
Related Topic