8

I have a custom module and i want to add remove every data including the increment id, using resource model in magento 2, i've read the solution in here but this is for magento 1, i'm not sure how to do it in magento 2, for example like this:

$model->getCollection()->truncate();

4 Answers 4

24

You can truncate table using DB Adapter and table name.

/** @var \Magento\Framework\DB\Adapter\AdapterInterface $connection */
$connection = $model->getResource()->getConnection();
$tableName = $model->getResource()->getMainTable();
$connection->truncateTable($tableName);

Alternatively you can get DB Adapter and table name from collection, but resource model rather.

/** @var \Magento\Framework\DB\Adapter\AdapterInterface $connection */
$connection = $model->getCollection()->getConnection();
$tableName = $model->getCollection()->getMainTable();
$connection->truncateTable($tableName);
3

Create a function in your ResourceModel

app\code\Module\Namespace\Model\ResourceModel

Inventory.php

<?php

namespace Module\Namespace\Model\ResourceModel;

class Inventory extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
     protected function _construct()
     {
          $this->_init('your table name', 'primary key column name');
     }
    public function truncateTable()
        {
            if ($this->getConnection()->getTransactionLevel() > 0) {
                $this->getConnection()->delete($this->getMainTable());
            } else {
                $this->getConnection()->truncateTable($this->getMainTable());
            }
    
            return $this;
        }
}

And call this function from any file in your custom module. This must delete the table related to the resource.

1

Magento 2 can't provide directly to truncate resource collection.

Use Install/Upgrade Data script by using setup command

DB Adapter : Magento\Framework\DB\Adapter\AdapterInterface::truncateTable($tableName, $schemaName)

app/code/[vender_name]/[module_name]/setup/InstallData.php

namespace <vender>\<module>\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * Category attribute Upgrade Data script
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{


    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $connection = $setup->getConnection();
        // truncate table
        $connection->truncateTable($tableName);

    }
}
2
  • are you sure? what about dynamic index tables, flat tables, report tables? Are we need to trancate tables only using setup? I doubt it. We can use collection object as way to retrieve adapter for table and tablename for execute trancation but you can use other way to fetch this objects.
    – Max
    Commented Jan 23, 2017 at 8:38
  • @MaxStsepantsevich getConnection() is DB adapter method, so no need to load collection for truncate any table. it's will use directly
    – Bhavik
    Commented Jan 23, 2017 at 8:48
1

If if want to truncate the custom table in your controller, Cron, and Block so you can first use create the modal/collection on their constructor and use the below structure for the same, and also you can use code as per your requirement.

use Vendor\Module\Model\ModelFactory;
.....
class Collect extends ....
{

protected $_modelFactory;
......

public function __construct(
   .....
   .....
    ModelFactory $modelFactory

) {
    .....
    $this->_modelFcatory = $modelFactory;
    
}
public function execute()
{
        $connection = $this->_modelFactory->create()->getCollection()->getConnection();
        $tableName = $this->_modelFactory->create()->getCollection()->getMainTable();
        $connection->truncateTable($tableName);
 }
}

Hope This helps You.

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.