Today, we will guide you on How to delete CMS Page and Block by Id programmatically in Magento 2. You can delete CMS Page and Block by id in Magento 2 using Repository interface.
Delete CMS Block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public function __construct( \Magento\Cms\Api\BlockRepositoryInterface $blockRepository ) { $this->blockRepository = $blockRepository; } public function deleteCmsBlock(int $blockId) { if($blockId){ try { $this->blockRepository->deleteById($blockId); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { } } } |
Delete CMS Page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public function __construct( \Magento\Cms\Api\PageRepositoryInterface $pageRepository ) { $this->pageRepository = $pageRepository; } public function deleteCmsPage(int $pageId) { if($pageId){ try { $this->pageRepository->deleteById($pageId); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { } } } |
Note: You just have to pass CMS page or block id in function to delete specific CMS Page or Block.
deleteCmsBlock(11);
deleteCmsPage(10)
Be First to Comment