Codeigniter – Delete Query

Delete function is used to delete the one or more row data from database table. If you want to delete all data from a table, you can use the truncate() function, or empty_table().

$this->db->delete()


$this->db->delete('tbl_user', array('id' => $id)); 
//DELETE FROM tbl_user WHERE id = $id

$this->db->where('id', $id);
$this->db->delete('tbl_user');
//DELETE FROM tbl_user WHERE id = $id
An array of table names can be passed into delete() if you would like to delete data from more than 1 table.

$id=5;
$tables = array('table1', 'table2', 'table3');
$this->db->where('id', $id);
$this->db->delete($tables);

$this->db->empty_table()


$this->db->empty_table('tbl_user'); 
// DELETE FROM tbl_user

$this->db->truncate()


$this->db->from('tbl_user'); 
$this->db->truncate(); 
(OR)
$this->db->truncate('tbl_user'); 
// TRUNCATE table tbl_user;

Delete With Join


$this->db->from("table1");
$this->db->join("table2", "table1.t1_id = table2.t2_id");
$this->db->where("table2.t2_id", $id);
$this->db->delete("table1");