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:

When to Use DELETE Requests

Use DELETE requests when you need to:

How to Make a DELETE Request

Using our Postboy, follow these steps to make a DELETE request:

  1. Select DELETE from the method dropdown
  2. Enter the URL of the resource you want to delete
  3. Add headers in the "Headers" tab if needed (e.g., Authorization)
  4. Optionally add a request body in the "Body" tab (some APIs require this)
  5. Click the "Send" button

The response will appear in the response section below.

Example DELETE Requests

Example 1: Delete a Resource

HTTP
DELETE https://jsonplaceholder.typicode.com/posts/1 HTTP/1.1
Host: jsonplaceholder.typicode.com

Response:

JSON
{}

Status: 200 OK - The resource was successfully deleted

Example 2: Delete with Authorization

HTTP
DELETE https://api.example.com/users/123 HTTP/1.1
Host: api.example.com
Authorization: Bearer your_token_here

Response:

JSON
{
  "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

HTTP
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
HTTP/1.1 204 No Content

Status: 204 No Content - Successfully deleted with no response body

Example 4: Delete with Request Body

HTTP
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:

JSON
{
  "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

Best Practices for DELETE Requests

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

JavaScript
// 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);