Working with RESTful services in Codeigniter – DELETE example


In this tutorial I am going to show you how we can work with RESTful or REST webservice in Codeigniter framework.

You may also be interested in Working with RESTful services in Codeigniter – GET example, Working with RESTful services in Codeigniter – POST example  and Working with RESTful services in Codeigniter – PUT example

The base/root URI for the Web service such as http://<host>/<appcontext/contextpath>/<url pattern>/<resources>.

The MIME type of the response data supported, which are JSON/XML/TEXT/HTML etc.

HTTP methods are mapped to CRUD (create, read, update and delete) actions for a resource. Although you can make slight modifications such as making the PUT method to create or update, the basic patterns are listed as follows.

HTTP DELETE: Delete a resource or collection of resources.


Please go through Setup RESTful service with Codeigniter


Step 1. Create a MySQL table contact under database roytuts.

USE `roytuts`;

/*Table structure for table `contact` */


CREATE TABLE `contact` (
  `contact_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `contact_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `contact_address` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
  `contact_phone` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`contact_id`)

Step 2. Dump some data into table contact.

insert  into 
(1,'S Roy','','1234578960'),
(2,'S Roy','','1234578960'),
(3,'S Roy','','5454544574'),
(4,'S Roy','','4578912360'),
(5,'S Roy','','8945761254'),
(6,'S Roy','','4587961235'),
(12,'S Roy','','1254897652'),
(13,'S Roy','Earth','4517895621');

Step 3. Modify <root directory>/application/config/autoload.php file for auto-loading html, url, file, form and database.

$autoload['helper'] = array('html', 'url', 'file');
$autoload['libraries'] = array('database');

modify also <root directory>/application/config/database.php.

'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'roytuts',
'dbdriver' => 'mysqli',

Step 4. Create a controller file RestDeleteController.php under <project root>/application/controllers with the following source code.

The actual function’s name is delete_contact because we are going to make HTTP DELETE request.

The _delete suffix in the function’s name is used to indicate a HTTP DELETE request.

use Restserver\Libraries\REST_Controller;

defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';

 * Description of RestDeleteController
 * @author
class RestDeleteController extends REST_Controller {

	use REST_Controller {
		REST_Controller::__construct as private __resTraitConstruct;
	function __construct() {
		$this->load->model('ContactModel', 'cm');

    function delete_contact_delete($contact_id) {

        $result = $this->cm->delete_contact($contact_id);

        if ($result === FALSE) {
            $this->response(array('status' => 'failed'));
        } else {
            $this->response(array('status' => 'success'));


Step 5. Create a model file ContactModel.php under <project root>/application/models with the following source code.


defined('BASEPATH') OR exit('No direct script access allowed');

 * Description of ContactModel
 * @author
class ContactModel extends CI_Model {

    private $contact = 'contact';

    function delete_contact($contact_id) {
        $this->db->where('contact_id', $contact_id);


Step 6. Now run the application on apache http server.

Step 7. Use the REST Client in Firefox browser to test the REST service.

Method: DELETE

URL: http://localhost/ci_3_1_0/index.php/restdeletecontroller/delete_contact/13

$[‘rest_default_format’]=’json’ at <root directory>/application/config/rest.php


    "status": "success"

Step 8. Check the database, you will see that row with contact_id=13 has been deleted successfully.

Thanks for reading.

3 thoughts on “Working with RESTful services in Codeigniter – DELETE example

  1. your rest service is delete_contact/13 and your controller is function delete_contact_delete($contact_id)

Leave a Reply

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