Last active
May 29, 2024 20:44
-
-
Save artttj/a39557b65958b14e152ee3b04d8b5c56 to your computer and use it in GitHub Desktop.
Script to Mass Delete Ghost Indices (Magento 2; smile/elasticsuite)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Magento\Framework\App\Bootstrap; | |
use Magento\Framework\App\State; | |
use Magento\Framework\ObjectManagerInterface; | |
require __DIR__ . '/app/bootstrap.php'; | |
class Test | |
{ | |
public function __construct( | |
private readonly ObjectManagerInterface $objectManager | |
) { | |
$state = $objectManager->get(State::class); | |
$state->setAreaCode('crontab'); | |
} | |
public function execute() | |
{ | |
$indexStatsProvider = $this->objectManager->get(\Smile\ElasticsuiteIndices\Model\IndexStatsProvider::class); | |
$indices = $indexStatsProvider->getElasticSuiteIndices(); | |
foreach ($indices as $name => $alias) { | |
$indexStats = $indexStatsProvider->indexStats($name, $alias); | |
if(isset($indexStats['index_status']) && $indexStats['index_status'] === \Smile\ElasticsuiteIndices\Block\Widget\Grid\Column\Renderer\IndexStatus::GHOST_STATUS) { | |
$indexStatsProvider->deleteIndex($name); | |
} | |
} | |
} | |
} | |
if (php_sapi_name() != 'cli') { | |
throw new \Exception('Script can only be invoked by shell'); | |
} | |
$class = new Test(Bootstrap::create(BP, $_SERVER)->getObjectManager()); | |
$class->execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment