DELETE Request Documentation
Learn how to make DELETE requests using our Postboy
What is a DELETE Request?
The HTTP DELETE method is used to delete a specified resource from the server. It removes the resource identified by the URI, and once deleted, subsequent requests to that resource should return a 404 Not Found or 410 Gone status.
Key characteristics of DELETE requests:
- Idempotent - multiple identical requests have the same effect as a single request
- Removes the specified resource from the server
- Not cached
- May or may not have a request body (typically doesn't)
- Successful deletion often returns 200, 202, or 204 status codes
- Safe to retry without additional side effects
When to Use DELETE Requests
Use DELETE requests when you need to:
- Remove a user account
- Delete a post, comment, or article
- Remove an item from a shopping cart
- Delete a file or image
- Revoke access tokens or sessions
- Remove a database record
How to Make a DELETE Request
Using our Postboy, follow these steps to make a DELETE request:
- Select
DELETEfrom the method dropdown - Enter the URL of the resource you want to delete
- Add headers in the "Headers" tab if needed (e.g., Authorization)
- Optionally add a request body in the "Body" tab (some APIs require this)
- Click the "Send" button
The response will appear in the response section below.
Example DELETE Requests
Example 1: Delete a Resource
DELETE https://jsonplaceholder.typicode.com/posts/1 HTTP/1.1
Host: jsonplaceholder.typicode.com
Response:
{}
Status: 200 OK - The resource was successfully deleted
Example 2: Delete with Authorization
DELETE https://api.example.com/users/123 HTTP/1.1
Host: api.example.com
Authorization: Bearer your_token_here
Response:
{
"message": "User successfully deleted",
"userId": 123,
"deletedAt": "2024-01-15T10:30:00Z"
}
Status: 200 OK - Deletion confirmed with response body
Example 3: Delete with No Content Response
DELETE https://api.store.com/cart/items/789 HTTP/1.1
Host: api.store.com
Authorization: Bearer your_token_here
Content-Type: application/json
Response:
HTTP/1.1 204 No Content
Status: 204 No Content - Successfully deleted with no response body
Example 4: Delete with Request Body
DELETE https://api.example.com/comments/456 HTTP/1.1
Host: api.example.com
Content-Type: application/json
Authorization: Bearer your_token_here
{
"reason": "spam",
"notifyUser": false
}
Response:
{
"message": "Comment deleted successfully",
"commentId": 456,
"reason": "spam"
}
Some APIs accept a request body with DELETE to provide additional context
Common HTTP Status Codes for DELETE Requests
- 200 OK - The resource was successfully deleted and a response body is returned
- 202 Accepted - The deletion request has been accepted but not yet completed
- 204 No Content - The resource was successfully deleted with no response body
- 400 Bad Request - The request was malformed or invalid
- 401 Unauthorized - Authentication is required and has failed or not been provided
- 403 Forbidden - The client does not have permission to delete the resource
- 404 Not Found - The resource to delete doesn't exist
- 405 Method Not Allowed - DELETE method is not allowed for this resource
- 409 Conflict - The deletion conflicts with the current state (e.g., resource is locked)
- 410 Gone - The resource was previously deleted and is no longer available
- 500 Internal Server Error - The server encountered an error while deleting
Best Practices for DELETE Requests
- Use proper authorization - Always verify the user has permission to delete the resource
- Be specific with URIs - Include the exact resource ID to avoid accidental deletions
- Implement soft deletes - Consider marking records as deleted instead of permanent removal
- Return appropriate status codes - Use 204 for no content or 200 with confirmation data
- Consider cascade effects - Think about related data that may need to be deleted
- Add confirmation mechanisms - For critical resources, require additional confirmation
- Log deletion activities - Keep audit trails for accountability
- Handle idempotency correctly - Return 404 or 410 for already-deleted resources
Soft Delete vs Hard Delete
Hard Delete - Permanently removes the data from the database. Cannot be recovered.
- Frees up storage space immediately
- No recovery possible
- Breaks referential integrity if not handled carefully
Soft Delete - Marks the data as deleted but keeps it in the database (e.g., with a deleted_at timestamp).
- Allows for data recovery
- Maintains audit trails
- Preserves referential integrity
- Can be permanently deleted later
Try It in JavaScript
Here's how you can make a DELETE request in JavaScript:
JavaScript Example
// Using fetch API
fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'DELETE'
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
// Some APIs return 204 No Content, which has no body
if (response.status === 204) {
console.log('Resource deleted successfully (no content)');
return null;
}
return response.json();
})
.then(data => {
if (data) {
console.log('Resource deleted:', data);
}
})
.catch(error => console.error('Error:', error));
// Using async/await with authorization
async function deleteResource(resourceId) {
try {
const response = await fetch(`https://api.example.com/posts/${resourceId}`, {
method: 'DELETE',
headers: {
'Authorization': 'Bearer your_token_here',
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// Handle different response types
if (response.status === 204) {
console.log('Resource deleted successfully');
return { success: true };
}
const data = await response.json();
console.log('Resource deleted:', data);
return data;
} catch (error) {
console.error('Error deleting resource:', error);
throw error;
}
}
// Example with confirmation
async function deleteWithConfirmation(resourceId) {
if (!confirm('Are you sure you want to delete this resource?')) {
return;
}
try {
await deleteResource(resourceId);
alert('Resource deleted successfully!');
} catch (error) {
alert('Failed to delete resource. Please try again.');
}
}
// Delete with request body (if API requires it)
async function deleteWithReason(resourceId, reason) {
try {
const response = await fetch(`https://api.example.com/items/${resourceId}`, {
method: 'DELETE',
headers: {
'Authorization': 'Bearer your_token_here',
'Content-Type': 'application/json'
},
body: JSON.stringify({
reason: reason,
timestamp: new Date().toISOString()
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error:', error);
throw error;
}
}
// Example usage
deleteWithConfirmation(123);