Previously, we learned to add delete action column in Magento 2.
If you wish to delete data from the table in Magento 2, this post is helpful for you.
Here, we will delete all the data of a user that is ID, name, email, telephone and created at in Magento 2.
The below solution is a part of Magento 2 module development series.
Steps to Delete Data from Table in Magento 2:
To delete the data from the table in Magento 2, you have to create a file Delete.php at appcodeMeetanshiExtensionControllerIndex
<?php namespace Meetanshi\Extension\Controller\Index; use Magento\Framework\App\Action\Context; use Magento\Framework\View\Result\PageFactory; use Meetanshi\Extension\Model\ExtensionFactory; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\App\Action\Action; class Delete extends Action { protected $resultPageFactory; protected $extensionFactory; public function __construct( Context $context, PageFactory $resultPageFactory, ExtensionFactory $extensionFactory ) { $this->resultPageFactory = $resultPageFactory; $this->extensionFactory = $extensionFactory; parent::__construct($context); } public function execute() { try { $data = (array)$this->getRequest()->getParams(); if ($data) { $model = $this->extensionFactory->create()->load($data['id']); $model->delete(); $this->messageManager->addSuccessMessage(__("Record Delete Successfully.")); } } catch (\Exception $e) { $this->messageManager->addErrorMessage($e, __("We can\'t delete record, Please try again.")); } $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setUrl($this->_redirect->getRefererUrl()); return $resultRedirect; } }
After creating the file, when you click Delete, you will manage to delete the data.

Done!
Do share the solution with Magento Community via social media.
Thank you.