You can delete CMS static block programmatically using deleteById() function of Magento\Cms\Api\BlockRepositoryInterface interface.
You just need to inject the interface in your constructor and you required only a Static Block id to delete a specific Block.
<?php use Magento\Cms\Api\BlockRepositoryInterface; use Magento\Framework\Exception\LocalizedException; public function __construct( BlockRepositoryInterface $blockRepository ) { $this->blockRepository = $blockRepository; } /** * @param int $blockId * @return bool */ public function deleteCmsBlock(int $blockId) { try { $result = $this->blockRepository->deleteById($blockId); } catch (LocalizedException $exception) { throw $exception->getMessage(); } return $result; }
You just have to pass the Static Block ID in the deleteCmsBlock() function.
This is the way to delete CMS Block programmatically in Magento with ease.
Hi Rakesh,
This block id is of type integer, so it will be the id which is saved in database table. Can we delete a block using the block identifier which we set from the admin ?