1

I'm able to fetch the single items, or the whole tables via the $modelFactory->create()->load(ID) or $modelFactory->create()->getCollection()

Using the $modelFactory, how can I delete all the data fetched?

---- UPDATE ----

The code I'm using at the moment is the following one. I guess it's running a query per item, instead of doing one query for all the items, so I'm looking for a way to do it in one single shot.

$cleanup = $this->myModelFactory->create()->getCollection();
foreach($cleanup as $item) {
   $item->delete();
}
  • What's wrong with the way you're doing it now? – Daniel Black Nov 8 '18 at 19:36
  • @DanielBlack is not wrong it's more something about performance. I expect that an SQL query will be run at each foreach recursion. So if for example $cleanup has 50 items, the foreach loop will generate 50 SQL delete query instead of one with all the items to delete. – Luigi T. Nov 8 '18 at 20:32
  • Why not run a direct SQL query? – André Ferraz Nov 20 '18 at 18:16
  • @acf it should be part of a process, so I'd like to use the magneto way as much as possible – Luigi T. Nov 20 '18 at 22:42
  • @LuigiT. You can still use SQL using the Resource Connection class and make it part of a process. – André Ferraz Nov 21 '18 at 11:35
1

https://github.com/magento/magento2/blob/6ea7d2d85cded3fa0fbcf4e7aa0dcd4edbf568a6/app/code/Magento/Newsletter/Controller/Adminhtml/Problem/Grid.php#L43

$cleanup = $this->myModelFactory->create()->getCollection();
$cleanup->walk('delete');

This was already answered on: https://magento.stackexchange.com/a/118128/36271

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.