CakePHP : Delete multiple records

With CakePHP, you can remove multiple records from a database table using the deleteAll() model method.

deleteAll() Usage

$this -> Model -> deleteAll($conditions, $cascade = true);

Lets say that you wanted to remove all the records for a specific user from the Item model. Below is a quick example.

deleteAll() Example

$this -> Item -> deleteAll(array('Item.user_id' => 13));

…where 13 would be the ID of the specific user.

Off course…you can completely truncate a table with the same method, this time removing all conditions. Below is an example of how to truncate a database table using CakePHP.

deleteAll() table truncate

$this -> Model -> deleteAll();
  • Peter Minne

    The “deleteAll” requires a condition as first parameter. So your example to truncate a table should be something like this:

    $this->Model->deleteAll(’1 = 1′, false);

    Personally I think it would make sense if the cake team would implement an additional “truncate” method.

  • http://test.com jets

    hey, nice tipics buddy… i used this & working great.

  • parmjeet

    I am new in cakephp .Thanks for the tips..

  • http://www.aromawebdesign.com vancouver web development

    Thanks working grate :)

  • http://rotepawan.blogspot.com/ Pawan Rote

    Hey, The above condition works, thanks for the tip.
    One more thing I want point out that same condition can be treated as SQL injection.

  • Synue

    Let’s not forget that $this->Model->deleteAll(’1 = 1′, false); will only delete the records from the table. You would still have the auto increment option set.

    To sucessfully truncate a table you can $this->ModelName->query(“TRUNCATE TABLE table_name;”);