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) {
  • What's wrong with the way you're doing it now? Nov 8, 2018 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, 2018 at 20:32
  • Why not run a direct SQL query? Nov 20, 2018 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, 2018 at 22:42
  • @LuigiT. You can still use SQL using the Resource Connection class and make it part of a process. Nov 21, 2018 at 11:35

1 Answer 1



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

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 and acknowledge you have read our privacy policy.

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