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.
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.
The main purpose of DELETE in a REST API is to remove an existing resource.
Examples include:
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.
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.
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.
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.
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.
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.
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.
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);
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);
};
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);
}
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.
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.
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.
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.
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.
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.
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.
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);
Postman can be used to test a DELETE API before connecting it to React Native.
In Postman:
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"
}
});
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.
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.
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.
| PATCH | DELETE |
|---|---|
| Modifies an existing resource | Removes an existing resource |
| Example: change student email | Example: remove student |
| PATCH /students/10 | DELETE /students/10 |
| 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 |
DELETE operations can have a major effect on application data, so the API should apply appropriate authorization and validation.
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.
Question: Which HTTP method is commonly used to remove an existing resource from a REST API?