Magento 2 – Remove Custom Attribute from Attribute Set Programmatically

attribute-setcustom-attributesmagento2

I am trying to create a custom attribute and add it to a custom created attribute set. The problem is that it is being added to the Default attribute set. How can I make it only appear in my attribute set?
Below is the code I am using in the upgrade script.

/* @var Magento\Eav\Setup\EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
if (version_compare($context->getVersion(), '1.0.2', '<')) {
    $data = array (
       'type' => 'string',
       'label' => 'Designer',
       'input' => 'text',
       'required' => false,
       'sort_order' => 3,
       'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
       'group' => 'Product Details',
       'used_in_product_listing' => true,
       'visible_on_front' => true,
    );
    $attributeSet = 'Test-Attribute-Set';

    $entityTypeId = $this->coreRegistry->registry('entityType');
    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'designer',
        $data
    );
    $eavSetup->addAttributeToSet(
        $entityTypeId,
        $attributeSet,
        'Product Details',
        'designer'
    );
}

Best Answer

Please use the following code. Detailed explanation click here. http://www.pearlbells.co.uk/delete-attribute-options-magento-2/

$eavConfig = $object_Manager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product', 'color');
$id = $attribute->getAttributeId();
$options = $attribute->getSource()->getAllOptions();

foreach ($options as $option) {
echo 'Delete label : '.$option['label'].PHP_EOL;  
$options['delete'][$option['value']] = true; 
$options['value'][$option['value']] = true;
}

$eavSetup = $object_Manager->get('\Magento\Eav\Setup\EavSetup');
$eavSetup->addAttributeOption($options);