Lesson 15 of 158 – DELETE Method
15%

DELETE Method

The DELETE method is an HTTP method commonly used to remove an existing resource from a server.

For example, if an administrator wants to remove a student from a student management system, the React Native application can send a DELETE request to the REST API.

Note: DELETE is commonly used when an existing resource needs to be removed from the server.

1. What is DELETE?

DELETE is an HTTP method used to request removal of a resource.


DELETE /api/students/10

Here, 10 can represent the ID of the student that needs to be removed.

2. Purpose of DELETE

The main purpose of DELETE in a REST API is to remove an existing resource.

Examples include:

  • Deleting a student
  • Deleting a user
  • Deleting a product
  • Deleting an order
  • Deleting a course
  • Deleting an old record

3. DELETE and REST API

A REST API commonly uses DELETE when an existing resource needs to be removed.


DELETE /api/students/10

The endpoint identifies the resource that the server should remove.

4. DELETE URL

A DELETE request commonly includes the resource ID in the URL.


DELETE /api/students/25

In this example, 25 identifies the student to be deleted.

5. DELETE Request Body

A DELETE request does not normally need a request body when the resource can be identified by the URL.


DELETE /api/students/25

The server can use the ID from the URL to identify the resource.

6. DELETE Request Example

A DELETE request can look like this:


DELETE /api/students/25 HTTP/1.1

The server receives the request and determines whether the identified resource can be deleted.

7. DELETE Response

After processing a DELETE request, the server sends an HTTP response.


{
    "success": true,
    "message": "Student deleted successfully"
}

The response can inform the mobile application about the result.

8. HTTP Status Code 200

A successful DELETE request can return 200 OK when the server returns a response body.


HTTP/1.1 200 OK

The response can contain a success message or other useful information.

9. HTTP Status Code 204

A server can also return 204 No Content when a DELETE request succeeds and there is no response body.


HTTP/1.1 204 No Content

The exact response status depends on the API design.

10. DELETE with React Native Fetch

React Native can send a DELETE request using the Fetch API.


const response = await fetch(
    "https://example.com/api/students/10",
    {
        method: "DELETE"
    }
);

const data = await response.json();

console.log(data);

11. DELETE with Async/Await

Async/await makes DELETE request code easier to read.


const deleteStudent = async (id) => {

    const response = await fetch(
        `https://example.com/api/students/${id}`,
        {
            method: "DELETE"
        }
    );

    const data = await response.json();

    console.log(data);
};

12. DELETE Error Handling

DELETE requests should handle network and server errors.


try {

    const response = await fetch(
        "https://example.com/api/students/10",
        {
            method: "DELETE"
        }
    );

    if (!response.ok) {
        throw new Error("Delete failed");
    }

    const data = await response.json();

} catch (error) {

    console.log(error);

}

13. DELETE Confirmation

Before deleting important data, a mobile application can ask the user for confirmation.


Are you sure you want to delete
this student?

Confirmation helps prevent accidental deletion caused by an unintentional button press.

14. DELETE Button in React Native

A React Native list can provide a delete button for each resource.


<Button
    title="Delete"
    onPress={() => deleteStudent(item.id)}
/>

The button can call a function that sends the DELETE request.

15. DELETE with PHP REST API

PHP can process a DELETE request and obtain the resource ID from the URL or routing logic.

For example:


DELETE /api/students/10

The API can then use the ID to locate the corresponding database record.

16. DELETE and MySQL

PHP can use a MySQL DELETE query to remove a record.


$stmt = $pdo->prepare(
    "DELETE FROM students
     WHERE id = ?"
);

$stmt->execute([$id]);

Prepared statements should be used when working with user-supplied values.

17. Checking the Resource Before DELETE

The API can first check whether the requested record exists.


SELECT id
FROM students
WHERE id = ?

If the record does not exist, the API can return an appropriate response instead of attempting the deletion.

18. Complete PHP DELETE Flow


DELETE Request
      ↓
PHP REST API
      ↓
Get Resource ID
      ↓
Validate ID
      ↓
Check Resource
      ↓
MySQL DELETE
      ↓
JSON Response
      ↓
React Native

This is the basic flow for deleting a resource through a REST API.

19. DELETE Response in PHP

After successfully deleting a record, PHP can return a JSON response.


header("Content-Type: application/json");

echo json_encode([
    "success" => true,
    "message" => "Student deleted successfully"
]);

React Native can use the response to update the screen.

20. DELETE and Axios

Axios provides a convenient method for sending DELETE requests.


import axios from "axios";

const response = await axios.delete(
    "https://example.com/api/students/10"
);

console.log(response.data);

21. DELETE and Postman

Postman can be used to test a DELETE API before connecting it to React Native.

In Postman:

  1. Select the DELETE method.
  2. Enter the API URL.
  3. Include the resource ID.
  4. Click Send.
  5. Check the response status.
  6. Check the response body if one is returned.

22. DELETE and Authentication

Delete operations are often protected so that only authorized users can remove resources.

A token can be sent using the Authorization header.


fetch("https://example.com/api/students/10", {
    method: "DELETE",
    headers: {
        "Authorization": "Bearer YOUR_TOKEN"
    }
});

23. DELETE in a Student Management App

A React Native student management application can use DELETE to remove a student.


DELETE /api/students/15

After successful deletion, the student can be removed from the displayed list.

24. Updating the Mobile List After DELETE

After deleting a student, the application can update its local state.


setStudents(
    students.filter(
        student => student.id !== id
    )
);

This removes the deleted student from the current list without necessarily requesting the complete list again.

25. DELETE and Soft Delete

Some applications do not physically remove records from the database. Instead, they use a soft delete approach.

For example, a database can contain a status field:


status = "deleted"

The API can then exclude deleted records from normal GET requests.

26. DELETE vs PATCH

PATCH DELETE
Modifies an existing resource Removes an existing resource
Example: change student email Example: remove student
PATCH /students/10 DELETE /students/10

27. DELETE vs POST

POST DELETE
Commonly creates a resource Commonly removes a resource
POST /students DELETE /students/10
Sends new resource data Usually identifies the resource to remove

28. DELETE Security Considerations

DELETE operations can have a major effect on application data, so the API should apply appropriate authorization and validation.

  • Authenticate the user.
  • Check user permissions.
  • Validate the resource ID.
  • Check whether the resource exists.
  • Use prepared SQL statements.
  • Consider confirmation for user-facing delete actions.
  • Return a clear API response.

29. Common DELETE Mistakes

  • Using the wrong resource ID.
  • Forgetting to specify DELETE as the method.
  • Using the wrong API URL.
  • Not checking whether the resource exists.
  • Not validating the ID.
  • Using unsafe SQL queries.
  • Allowing unauthorized users to delete records.
  • Not handling network errors.
  • Not updating the mobile application list after deletion.
  • Accidentally deleting the wrong record.

30. Complete DELETE Concept

DELETE is an important HTTP method for removing existing resources from a REST API.


React Native
      ↓
DELETE /api/students/10
      ↓
PHP REST API
      ↓
Validate ID
      ↓
Check Student
      ↓
MySQL DELETE
      ↓
JSON Response
      ↓
React Native
      ↓
Update Student List

The mobile application sends the resource ID, the PHP API validates the request, the database operation removes the record according to the application's design, and the API returns a response.

📌 Key Points

  • DELETE is commonly used to remove a resource.
  • A resource ID is commonly included in the DELETE URL.
  • DELETE usually does not require a request body when the resource is identified by the URL.
  • React Native can send DELETE requests using Fetch or Axios.
  • PHP can process DELETE requests through the REST API.
  • PHP can use a MySQL DELETE query to remove a record.
  • Prepared statements should be used for database operations.
  • The API should validate the resource ID.
  • The API should check whether the resource exists.
  • Delete endpoints should normally use appropriate authorization.
  • Postman can be used to test DELETE APIs.
  • Some applications use soft delete instead of physically deleting database records.
  • The mobile application should update its list after a successful deletion.

🧠 Quick Quiz

Question: Which HTTP method is commonly used to remove an existing resource from a REST API?