I made some changes to the original script from @Kishor Thummar to improve performance and to log information while the script is running. You have to put the script in your webroot which is pub
if you configured your server properly.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
ini_set('max_execution_time', '18000');
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
require '../app/bootstrap.php'; // use "app/bootstrap.php" if your root is not set to pub folder (it should be though!)
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test_debug.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info("Script started.");
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository');
$categoryId = 3; // YOUR CATEGORY ID
$category = $categoryFactory->create()->load($categoryId);
// Set filter, so that only configurable products are collected (faster).
$categoryProducts = $category->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('type_id', array('eq' => 'configurable'));
foreach ($categoryProducts as $product) {
$logger->info($product->getName());
$_configChild = $product->getTypeInstance()->getUsedProductIds($product);
foreach ($_configChild as $childId){
//Delete child products
$childProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($childId);
$childProduct->delete();
$logger->info("Child simple deleted - '". $childProduct->getName());
}
//Delete Configurable product
$configurableProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId());
$configurableProduct->delete();
$logger->info('Configurable deleted - '.$configurableProduct->getName());
}
$logger->info("Script finished.");
Now you can run the script and watch the logfile with tail -f var/log/test_debug.log