{"id":1339,"date":"2020-11-06T07:06:28","date_gmt":"2020-11-06T07:06:28","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/11\/06\/add-delete-action-column-in-magento-2\/"},"modified":"2025-05-21T17:45:23","modified_gmt":"2025-05-21T12:15:23","slug":"add-delete-action-column-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-delete-action-column-in-magento-2\/","title":{"rendered":"How to Add Delete Action Column in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Alright, so the&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/display-table-data-magento-2\/\">previous lecture<\/a>&nbsp;was all about displaying table data on the frontend. You may think, is it possible to delete which is saved and displayed on the frontend?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. It is possible and simple too.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By adding delete column, one can delete the data easily. Or you can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/how-to-add-action-column-in-admin-grid-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">add action column to admin UI grid<\/a>&nbsp;with delete and edit options.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Programmatic Solution to Add Delete Action Column in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Change code in Showdata.php file at&nbsp;<em><strong>appcodeMeetanshiExtensionBlock<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\Extension\\Block;\n\nuse Magento\\Framework\\View\\Element\\Template;\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Meetanshi\\Extension\\Model\\ResourceModel\\Extension\\CollectionFactory;\n\nclass Showdata extends Template\n{\n\n    public $collection;\n\n    public function __construct(Context $context, CollectionFactory $collectionFactory, array $data = [])\n    {\n        $this->collection = $collectionFactory;\n        parent::__construct($context, $data);\n    }\n\n    public function getCollection()\n    {\n        return $this->collection->create();\n    }\n\n    public function getDeleteAction()\n    {\n        return $this->getUrl('extension\/index\/delete', ['_secure' => true]);\n    }\n\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Update&nbsp;<strong>showdata.phtml<\/strong>&nbsp;at&nbsp;<em><strong>appcodeMeetanshiExtensionviewfrontendtemplates<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n    $collection = $block->getCollection();\nif ($collection->count()) {\n?>\n&lt;div class=\"table-wrapper orders-history\">\n    &lt;table class=\"data table table-order-items history\" id=\"my-orders-table\">\n        &lt;caption class=\"table-caption\">&lt;?php echo __('Grid Record') ?>&lt;\/caption>\n        &lt;thead>\n        &lt;tr>\n            &lt;th scope=\"col\" class=\"col id\">&lt;?php echo __('ID') ?>&lt;\/th>\n            &lt;th scope=\"col\" class=\"col name\">&lt;?php echo __('Name') ?>&lt;\/th>\n            &lt;th scope=\"col\" class=\"col email\">&lt;?php echo __('Email') ?>&lt;\/th>\n            &lt;th scope=\"col\" class=\"col telephone\">&lt;?php echo __('Telephone') ?>&lt;\/th>\n            &lt;th scope=\"col\" class=\"col createat\">&lt;?php echo __('Created At') ?>&lt;\/th>\n            &lt;th scope=\"col\" class=\"col action\">&lt;?php echo __('Action') ?>&lt;\/th>\n        &lt;\/tr>\n        &lt;\/thead>\n        &lt;tbody>\n        &lt;?php\n                foreach ($collection as $item): ?>\n        &lt;tr>\n            &lt;td data-th=\"&lt;?= $block->escapeHtml(__('ID')) ?>\" class=\"col id\">\n                &lt;?php echo $item->getId() ?>\n            &lt;\/td>\n            &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Name')) ?>\" class=\"col name\">\n                &lt;?php echo $item->getName() ?>\n            &lt;\/td>\n            &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Email')) ?>\" class=\"col email\">\n                &lt;?php echo $item->getEmail() ?>\n            &lt;\/td>\n            &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Telephone')) ?>\" class=\"col telephone\">\n                &lt;?php echo $item->getTelephone() ?>\n            &lt;\/td>\n            &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Created At')) ?>\"\n                class=\"col title\">&lt;?php echo date('Y-m-d', strtotime($item->getCreatedAt())); ?>\n            &lt;\/td>\n            &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Action')) ?>\" class=\"col delete\">\n                &lt;a href=\"&lt;?php echo $block->getDeleteAction() . 'id\/' . $item->getId(); ?>\">&lt;?php echo __('Delete') ?>&lt;\/a>\n            &lt;\/td>\n        &lt;\/tr>\n        &lt;?php endforeach; ?>\n        &lt;\/tbody>\n    &lt;\/table>\n&lt;\/div>\n&lt;?php\n    }\n    ?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After making changes in Showdata.php and showdata.phtml files, you will manage to add \u2018delete column\u2019 in Magento 2.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-Add-Delete-Column-in-Magento-2-1024x288.jpg\" alt=\"How to Add 'Delete Column' in Magento 2\" class=\"wp-image-11231\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Share with post with newbie Magento developers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/meetanshi.com\/blog\/display-table-data-magento-2\/\">How to Display Table Data in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/delete-data-from-table-in-magento-2\/\">How to Delete Data from Table in Magento 2<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Alright, so the&nbsp;previous lecture&nbsp;was all about displaying table data on the frontend. You may think, is it possible to delete which is saved and displayed&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1339","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1339"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1339\/revisions"}],"predecessor-version":[{"id":13940,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1339\/revisions\/13940"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}