1

When i'm trying to delete all records from Listing, By selecting all. Not able to delete.

app\code\Custom\Module\Controller\Adminhtml\Messages\MassDelete.php

namespace Custom\Module\Controller\Adminhtml\Messages;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Custom\Module\Model\ResourceModel\Messages\CollectionFactory;
use Magento\Framework\Controller\ResultFactory;


class MassDelete extends \Magento\Backend\App\Action {

/**
 * @var Filter
 */
protected $filter;

/**
 * @var CollectionFactory
 */
protected $collectionFactory;

/**
 * @param Context $context
 * @param Filter $filter
 * @param CollectionFactory $collectionFactory
 */
public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory) {
    $this->filter = $filter;
    $this->collectionFactory = $collectionFactory;
    parent::__construct($context);
}

/**
 * Execute action
 *
 * @return \Magento\Backend\Model\View\Result\Redirect
 * @throws \Magento\Framework\Exception\LocalizedException|\Exception
 */
public function execute() {

     $collection = $this->filter->getCollection($this->collectionFactory->create());
      $collectionSize = $collection->getSize();

      foreach ($collection as $item) {
      $item->delete();
      } 

    $this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $collectionSize));

    /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
    return $resultRedirect->setPath('*/*/');
}
}

It always give blank, when i'm trying to print $collection

[Solution]

Thanks to this post Magento 2 massaction delete not working

Only missing

Check if you have following line in your Model\ResourceModel\Module\Collection.php file:

protected $_idFieldName = 'module_id';

closed as off-topic by Marius Sep 2 '16 at 7:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced OR seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Marius
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please update your Ui component. – Khoa TruongDinh Aug 29 '16 at 3:14
  • I'm using use Magento\Ui\Component\MassAction\Filter; how? – Ankit Shah Aug 29 '16 at 3:15
  • You have declared your Ui component? – Khoa TruongDinh Aug 29 '16 at 3:17
  • @KhoaTruongDinh I have added whole page code in question. Now please look in to that – Ankit Shah Aug 29 '16 at 3:19
  • How about your current issue? – Khoa TruongDinh Aug 29 '16 at 15:16