Sign up ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I have a custom table. I want to truncate the table using Magento collection without SQL query.

Hope someone will provide some useful information.

share|improve this question
Do you mean you want to delete some rows based on some condition? – Subesh Pokhrel Mar 3 '14 at 12:26
No, I need to delete all records and also Auto Increment id start with 1. – Vijay S Mar 3 '14 at 12:56

3 Answers 3

Magento does not have a support for this (as far as I know).
But you can implement a method in your resource model (not the collection resource model) that will truncate the table.
Something like this:

public function truncate() {
    $this->_getWriteAdapter()->query('TRUNCATE TABLE '.$this->getMainTable());
    return $this;

Then you can call it in your code:


But this is a very dangerous approach. The truncate statement breaks the transaction, so it cannot be rolled back in case you need to.
I suggest deleteing every entity in the table instead.

$collection = Mage::getModel('[module]/[entity]')->getCollection();
foreach ($collection as $item) {

The down side of this is that it doesn't reset the increment id of the table. But it's safer.

To update the increment id of the main table you can add a new method in your resource model that does something like this.

public function changeAutoIncrement($increment = 1) {
    $this->_getWriteAdapter()->query('ALTER TABLE '.$this->getMainTable().' AUTO_INCREMENT = '. $increment);

Then call your method in the code:

share|improve this answer
could you tell me three types of models in magento.thanks in advance – MeenakshiSundaram R Mar 3 '14 at 12:42
@MeenakshiSundaramR. There is the entity model. Example: Mage_Catalog_Model_Product. There is the resource model. Example: Mage_Catalog_Model_Resource_Product. And there is the collection resource model Mage_Catalog_Model_Resource_Product_Collection. – Marius Mar 3 '14 at 12:47
@Marius Can you tell me How to update auto increment id ( its means start with 1 ) available in magento collection. – Vijay S Mar 3 '14 at 13:03
@VijayS. See my update to the answer. – Marius Mar 3 '14 at 13:08
@Marius Thanks....... – Vijay S Mar 3 '14 at 13:09

Here's the link if you want to delete rows based on collection if that's what you mean.

share|improve this answer
he is asking about truncate the table. Not to delete the record – MeenakshiSundaram R Mar 3 '14 at 12:32

I have create a Model and for some development i had to implement delete method. It is very easy. I also try for conditional delete but i didn't get exact code. But i am using the following code which will fulfill your requirement for conditional delete.

$items = Mage::getModel('custom/csaabandonedcart')->getCollection()
->getCustomerId())->addFieldToFilter('customer_id', $retailer)
->addFieldToFilter('product_id', $spid);
foreach($items as $item){
share|improve this answer
I am asking about truncate the table. Not to delete the record. – Vijay S Oct 14 at 8:10
you can do using the following code. if you consider the above code reference. $items->truncate(); – Sapnandu Oct 29 at 5:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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