Codeigniter Delete Query Example

Codeigniter delete query example : Codeigniter inbult query bulder $this->db->delete() is used to delete the data from table. Codeigniter also provides $this->db->empty_table() and $this->db->truncate() method to delete all data from table. $this->db->delete() deletes data based on where condition, it will delete the rows where condition is fullfilled. Let us understand how we can use these query builders to delete data from tables.

Delete data in Codeigniter | Truncate Table | Remove All Data


$this->db->delete() is used to delete data in codeigniter table.

Codeigniter delete query example

Let us go one by one to understand the delete functionality in Codeigniter.

Delete With Where Condition

You can delete the data based on condition using the below example-

$where_array = array(
                'id'=>$id
            );

$this->db->delete('table_name', $where_array);

or You can also use this :

$where_array = array(
                'id'=>$id
            );
$this->db->where($where_array);
$this->db->delete('table_name');

This will generate the following Query :


 // Delete from table_name WHERE id = $id

Using where condition you can delete Multiple Rows from table.

Empty Table | Delete All Rows

You can use the following syntax to empty any table. It will delete all rows from table.

$this->db->empty_table('users_table');

It will produce query like this – DELETE from users_table

Note: – It will delete all records from table so before running the above query make sure you really want to delete all rows.

Truncate Table

$this->db->truncate('users_table');

It will produce query like this – TRUNCATE users_table;

Note: – It will truncate table & remove all indexes as well so make sure you really want to truncate table.


Advertisements

Add Comment

📖 Read More