For any queries you can reach us at / WhatsApp us: +919158876092

Delete data in Database using CodeIgniter 4


In this example, we will discuss how to delete a record or data from the MySQL database using the CodeIgniter.

Controller: 	User.php		app/Controllers/User.php
Model:		UserModel.php		app/Models/UserModel.php
View:		list.php		app/Views/list.php


$routes->add('user/list', 'User::list');
$routes->get('user/delete/(:num)', 'User::delete/$1');


namespace App\Controllers;

use App\Models\UserModel;

class User extends BaseController {

	public function __construct() {

		$db = db_connect();
		$this->userModel = new UserModel($db);

	public function add() {

	public function list() {
		$data['result']	= $this->userModel->list();
		echo view('list', $data);

	public function delete($id) {
		$result = $this->userModel->delete($id);
		if($result) {
			echo "User record is deleted successfully.";
		} else {
			echo "Something went wrong";



namespace App\Models;

use CodeIgniter\Model;
use CodeIgniter\Database\ConnectionInterface;

class UserModel extends Model {
	protected $db;
	public function __construct(ConnectionInterface &$db) {
		$this->db =& $db;

	public function list() {
		return $this->db

	public function delete($id) {
		return $this->db
                        ->where(["id" => $id])


<table width="600" border="1" cellspacing="5" cellpadding="5">
	<tr style="background:#CCC">
		<th>Sr No</th>
		<th>Email Id</th>
	<?php $i=1; foreach($result as $row) { 
		echo "<tr>"; 
		echo "<td>".$i. "</td>"; 
		echo "<td>".$row->first_name."</td>";
		echo "<td>".$row->last_name."</td>"; 
		echo "<td>".$row->email."</td>"; 
		echo "<td><a href="<?php echo base_url('user/delete'.$row->id) ?>">Delete</a></td>";
		echo "</tr>"; $i++; } ?>