DELETE Requests

Introduction

A DELETE request is an HTTP request used to remove an existing resource from a server. It is one of the standard HTTP methods defined by the HTTP protocol and is commonly used in REST APIs to delete data.

Unlike GET, which retrieves data, POST, which creates data, and PUT, which updates data, the DELETE method permanently removes a resource from the server (depending on how the API is designed).

DELETE requests are widely used for deleting user accounts, removing products, canceling orders, deleting blog posts, clearing notifications, and removing database records.

For automation engineers, DELETE requests are essential for testing API endpoints that remove resources, validating response codes, verifying successful deletion, and ensuring applications correctly handle delete operations.

In this tutorial, you’ll learn how to send DELETE requests using JavaScript with both the Fetch API and Axios.


What is a DELETE Request?

A DELETE request is an HTTP request used to remove an existing resource from a server.

The resource to delete is usually identified by its URL.


Why Use DELETE Requests?

DELETE requests help developers:

  • Remove users.

  • Delete products.

  • Cancel orders.

  • Delete blog posts.

  • Remove files.

  • Delete database records.

  • Maintain application data.


Basic Syntax Using Fetch API

fetch(

    "API_URL",

    {

        method: "DELETE"

    }

);

Basic Syntax Using Axios

import axios

from "axios";

await axios.delete(

    "API_URL"

);

Example 1: Delete a Post Using Fetch API

async function deletePost() {

    const response =

        await fetch(

            "https://jsonplaceholder.typicode.com/posts/1",

            {

                method: "DELETE"

            }

        );

    console.log(

        response.status

    );

}

deletePost();

Sample Output

200

Example 2: Delete a User Using Axios

import axios

from "axios";

async function deleteUser() {

    const response =

        await axios.delete(

            "https://jsonplaceholder.typicode.com/users/1"

        );

    console.log(

        response.status

    );

}

deleteUser();

Sample Output

200

Example 3: Delete a Product

fetch(

    "https://jsonplaceholder.typicode.com/posts/10",

    {

        method: "DELETE"

    }

)

.then(response =>

    console.log(

        response.status

    )

);

Sample Output

200

Example 4: Delete Resource Using Axios

import axios

from "axios";

async function removeResource() {

    const response =

        await axios.delete(

            "https://jsonplaceholder.typicode.com/posts/5"

        );

    console.log(

        response.status

    );

}

removeResource();

Sample Output

200

Example 5: Verify Successful Deletion

async function verifyDelete() {

    const response =

        await fetch(

            "https://jsonplaceholder.typicode.com/posts/3",

            {

                method: "DELETE"

            }

        );

    if (

        response.status === 200

    ) {

        console.log(

            "Resource deleted successfully."

        );

    }

}

verifyDelete();

Sample Output

Resource deleted successfully.

Automation Testing Examples

DELETE requests are widely used in API automation.

Example 1: Validate Status Code

const response =

    await axios.delete(

        "https://jsonplaceholder.typicode.com/posts/1"

    );

console.log(

    response.status

);

Sample Output

200

Example 2: Verify Successful Deletion

const response =

    await axios.delete(

        "https://jsonplaceholder.typicode.com/posts/2"

    );

if (

    response.status === 200

) {

    console.log(

        "Deleted"

    );

}

Sample Output

Deleted

Example 3: Measure Response Time

const start =

    Date.now();

await axios.delete(

    "https://jsonplaceholder.typicode.com/posts/4"

);

const end =

    Date.now();

console.log(

    end - start,

    "ms"

);

Sample Output

125 ms

Example 4: Delete Multiple Resources

for (

    let id = 1;

    id <= 3;

    id++

) {

    await axios.delete(

        `https://jsonplaceholder.typicode.com/posts/${id}`

    );

}

console.log(

    "Resources deleted."

);

Sample Output

Resources deleted.

Example 5: Handle Delete Errors

try {

    await axios.delete(

        "https://jsonplaceholder.typicode.com/posts/9999"

    );

}

catch (error) {

    console.log(

        "Delete failed."

    );

}

Sample Output

Delete failed.

Note: Whether this example throws an error depends on how the API handles requests for non-existent resources. Some APIs may return a success status instead of an error.


DELETE Request Components

ComponentPurpose
URLResource to delete
MethodDELETE
HeadersOptional request information
BodyUsually not required
ResponseDelete confirmation

DELETE vs Other HTTP Methods

MethodPurpose
GETRetrieve data
POSTCreate new resource
PUTReplace existing resource
PATCHPartially update resource
DELETERemove resource

Real-World Uses of DELETE Requests

DELETE requests are commonly used for:

  • Deleting user accounts.

  • Removing products.

  • Canceling orders.

  • Deleting blog posts.

  • Removing comments.

  • Clearing notifications.

  • Deleting uploaded files.

  • Removing database records.

  • API automation.

  • CRUD operations.


Common Mistakes

Deleting the Wrong Resource

Always verify the resource ID before sending a DELETE request.


Ignoring Response Status Codes

Validate that the server returns the expected status code after the deletion request.


Assuming Every API Returns the Same Response

Some APIs return 200 OK, others return 202 Accepted, 204 No Content, or different success codes depending on their design.


Not Handling Errors

Use try...catch to handle network failures and unexpected server responses.


Best Practices

  • Validate response status codes.

  • Verify that the resource is actually deleted.

  • Handle exceptions using try...catch.

  • Store API URLs in configuration files.

  • Keep delete logic reusable.

  • Log deletion results during automation testing.

  • Avoid deleting production data during testing.


Conclusion

DELETE requests are an essential part of REST APIs, allowing applications to remove resources safely and efficiently. They play a critical role in CRUD operations and are commonly used in modern web applications.

For automation engineers, DELETE requests are vital for validating resource removal, testing cleanup operations, verifying status codes, and ensuring backend services correctly process delete requests. Mastering DELETE requests with the Fetch API and Axios is an important step toward building complete and reliable API automation frameworks.


Frequently Asked Questions (FAQs)

What is a DELETE request?

A DELETE request is an HTTP request used to remove an existing resource from a server.


Does a DELETE request require a request body?

Usually, no. Most APIs identify the resource to delete using the URL.


Which status code indicates a successful DELETE request?

Many APIs return 200 OK or 204 No Content, although other success codes may also be used depending on the API design.


Can I use both Fetch API and Axios for DELETE requests?

Yes. Both support sending DELETE requests.


Why are DELETE requests important in automation testing?

They help verify delete operations, validate API behavior, clean up test data, and ensure backend services correctly remove resources.


Key Takeaways

  • DELETE requests remove existing resources.

  • The resource is typically identified by its URL.

  • Fetch API and Axios both support DELETE requests.

  • DELETE requests usually do not require a request body.

  • Always validate response status codes.

  • Verify that the resource has been removed successfully.

  • Handle errors using try...catch.

  • Different APIs may return different success status codes.

  • DELETE requests are an essential part of REST API CRUD operations.

  • Mastering DELETE requests is important for JavaScript and Node.js API automation.