You can Delete Stock by Stock Id in Multi-Source Inventory (MSI) Magento 2 programmatically by passing stock id only.
Delete the Stock data by Stock id. If the stock is not found do nothing.
Interface Used to delete stock, Magento\InventoryApi\Api\StockRepositoryInterface.
Method DeleteById($stockid) used to delete specific Stock from the MSI.
<?
namespace Jesadiya\DeleteStock\Model;
use Exception;
use Psr\Log\LoggerInterface;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
class DeleteStockById
{
/**
* @var StockRepositoryInterface
*/
private $stockRepository;
public function __construct(
StockRepositoryInterface $stockRepository,
LoggerInterface $logger
) {
$this->stockRepository = $stockRepository;
$this->logger = $logger;
}
/**
* @return void
*/
public function deleteStock()
{
try {
$stockId = 2;
$this->stockRepository->deleteById($stockId);
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
}
}
}
Call $this->deleteStock() method to delete specific stock from the MSI module.
Check the Stock is delete or not from the Magento Admin Panel Stock Grid.
