0

I want to delete all custom attribute sets, I'm facing an issue when I'm deleting attribute set manually then 404 error is showing in Magento 2.3

and can i delete all categories with 1 database query and start it from id 1 ?

1 Answer 1

1

You can use execute below script on your website to remove the attribute sets programmatically.

 <?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

//require __DIR__ . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');

 $attributeSetIds = [1,2,3,4]; //add attribute set ids here to delete

 $attributeSetRepository = $objectManager->create('\Magento\Catalog\Api\AttributeSetRepositoryInterface');
 foreach ($attributeSetIds as $attributeSetId) {
    $attributeSetRepository->deleteById($attributeSetId);
 }

And you can use below SQL query to remove categories from ID 1 on your website.

SET FOREIGN_KEY_CHECKS = 0;

TRUNCATE TABLE catalog_category_entity;

TRUNCATE TABLE catalog_category_entity_datetime; 
TRUNCATE TABLE catalog_category_entity_decimal; 
TRUNCATE TABLE catalog_category_entity_int; 
TRUNCATE TABLE catalog_category_entity_text; 
TRUNCATE TABLE catalog_category_entity_varchar; 
TRUNCATE TABLE catalog_category_product; 
TRUNCATE TABLE catalog_category_product_index;

INSERT INTO `catalog_category_entity` (`entity_id`, `attribute_set_id`, `parent_id`, `created_at`, `updated_at`, `path`, `position`, `level`, `children_count`) VALUES ('1', '0', '0', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '1', '0', '0', '1'),
('2', '3', '1', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '1/2', '1', '1', '0');

INSERT INTO `catalog_category_entity_int` (`value_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES 
('1', '69', '0', '1', '1'),
('2', '46', '0', '2', '1'),
('3', '69', '0', '2', '1');

INSERT INTO `catalog_category_entity_varchar` (`value_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES 
('1', '45', '0', '1', 'Root Catalog'),
('2', '45', '0', '2', 'Default Category');

SET FOREIGN_KEY_CHECKS = 1;

DELETE FROM url_rewrite WHERE entity_type = 'category';

Hope it helps!!!

8
  • both solutions are not working. Sep 12, 2019 at 13:37
  • have you got any errors??
    – Sumit
    Sep 12, 2019 at 13:38
  • Yes, after running of both categories queries in database, now in admin when i click on category then website is going out site cannot reached. and also attribute i'm getting Fatal error: Uncaught ReflectionException: Class MagentoFrameworkAppState does not exist in Sep 12, 2019 at 13:40
  • 1
    updated my code for the script, please check.
    – Sumit
    Sep 12, 2019 at 13:49
  • 1
    Yes, i update your code little bit about directory of bootstrap file. and now it's working fine. Thanks Sep 12, 2019 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.