CodeIgniter – Delete

Codeigniter – Delete

This tutorial will show you how to delete record from database using Codeigniter.

If you are new here and did not see previous tutorials, you may want to visit

Linux Web Zone CodeIgniter Series

There you can find links for all lessons from our CodeIgniter Series


We will edit Site.php controller and add new function called delete.

delete function is located after edit function from CodeIgniter – Update tutorial.

Our delete function will look like this


public function delete()
        $componentId = $this->uri->segment(2);




Additional Settings

Open routes.php file located in application/config directory.

At the end of the file add this line

$route['delete/(:num)'] = "site/delete/$1";

This will make our link to edit component look like


Instead of


This is optional and if you decide to skip this step, you need to edit Site.php Controller and change this line

        $componentId = $this->uri->segment(2);

To this

        $componentId = $this->uri->segment(3);

This will take the third segment of URL – site / delete / (id)



In Component.php Model we need a function that will delete record from database.

It will remove from database component with given ComponentID.

I’ll call it delete and it looks like this


function delete($id)
        $this->db->where('ComponentID', $id);



We’ll only add option for deleting component in table.

There is no need for an additional views because everything goes through controller and model so we only need URL to call controllers function for deleting record.


<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<?php $this->load->view('templates/header'); ?>

    <!-- Begin page content -->
    <div class="container">
        <div class="page-header">
            <h1>Hardware Components</h1>

        <div class="col-md-10">
            <div class="table-responsive">
                <table class="table table-condensed table-hover">
                        <th>Component Name</th>

                    <?php if(empty($components)) echo '<tr><td colspan="4"><em>There are no added components!</em></td></tr>'; ?>

                    <?php foreach ($components as $row): ?>
                            <td><?php echo $row->ComponentID; ?></td>
                            <td><?php echo $row->ComponentName; ?></td>
                            <td><?php echo $row->ComponentNo; ?></td>
                            <td><a href="<?php echo base_url() . 'edit/' . $row->ComponentID; ?>">Edit</a> | <a href="<?php echo base_url() . 'delete/' . $row->ComponentID; ?>">Delete</a></td>
                    <?php endforeach; ?>


<?php $this->load->view('templates/footer'); ?>


Our homepage will look like this now


After we click Delete link, record will be deleted from database and we’ll be returned to homepage where there is one component less


This completes Lesson 4 of our Linux Web Zone CodeIgniter Series

Stay tuned because we are only warming up for things to come.


You can download complete source code from this tutorial on the link bellow

CodeIgniter Lesson 4 – Delete

Leave a Reply

Your email address will not be published. Required fields are marked *