Q&A
What is Node request.delete()?
Overview
HTTP DELETE is used to ask a server to remove a resource. In Node.js this means making an outgoing HTTP request - request.delete() was the request-library idiom for this. Since request was deprecated in 2020, modern code uses fetch, axios, or node-fetch. Understanding the method and its modern equivalents is the practical focus.
graph LR
A["request.delete(url, cb)"] --> B["Sends HTTP DELETE to the URL"]
B --> C["Server removes the resource"]
C --> D["Callback / promise with response"]
A -.modern equivalents.-> E["fetch(url, {method:'DELETE'})"]
A -.modern equivalents.-> F["axios.delete(url)"]
style E fill:#1e1b4b,color:#fff
Using request.delete() (legacy, for reference)
const request = require("request");
request.delete("https://api.example.com/users/42", (err, response, body) => {
if (err) { console.error(err); return; }
console.log(response.statusCode); // 204 No Content (typical for DELETE)
console.log(body);
});
Note: request.delete is an alias for request.del (since delete is a JS keyword, older code used .del()).
With options (headers, auth)
request.delete({
url: "https://api.example.com/items/99",
headers: { "Authorization": "Bearer token123" },
json: true,
}, (err, res, body) => {
console.log(res.statusCode);
});
Modern equivalent 1: fetch (built-in, Node 18+)
const res = await fetch("https://api.example.com/users/42", {
method: "DELETE",
headers: { "Authorization": "Bearer token123" },
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
// 204 responses have no body:
if (res.status !== 204) {
const data = await res.json();
console.log(data);
}
fetch is now the preferred built-in for making HTTP requests in Node 18+ (and the browser). No package needed.
Modern equivalent 2: axios.delete()
const axios = require("axios");
const res = await axios.delete("https://api.example.com/users/42", {
headers: { Authorization: "Bearer token123" },
});
console.log(res.status); // 204
console.log(res.data); // empty for 204, or JSON for 200
axios has a cleaner API than request, promise-based by default, and still widely used.
HTTP DELETE semantics
| Aspect | Behavior |
|---|---|
| Idempotent? | Yes - deleting something twice should give the same result |
| Typical success codes | 204 No Content (deleted, no body) or 200 OK (with confirmation) |
| Typical error codes | 404 Not Found, 403 Forbidden, 409 Conflict |
| Body | Usually none in DELETE request; response may or may not have body |
Why the request Library Is Deprecated
request was the dominant HTTP client for Node from ~2010 but stopped receiving updates in 2020. Reasons: callback-based API pre-dates Promises/async-await, large/complex codebase, security maintenance burden. Modern replacements: fetch (built-in v18+), axios, got, node-fetch (for older Node), undici.
Why This Matters in Interviews
Node.js/API interviews use this to check HTTP method knowledge and library awareness. Key points: DELETE removes a resource, returns 204/200; the request library is deprecated - modern code uses fetch or axios; DELETE is idempotent; and sending authentication headers is standard for protected resources. Demonstrating the fetch equivalent (rather than just the deprecated API) signals current knowledge.
Common Pitfalls
- Still using the deprecated
requestlibrary in new code. - Not handling
404(resource already gone) - a DELETE should often succeed even if the resource is already absent (idempotency). - Expecting a response body on
204 No Content- calling.json()on it throws. - Forgetting authentication headers on protected DELETE endpoints.
- Confusing
request.delvsrequest.delete(they're aliases, butdeleteis a keyword).
References
requestlibrary (deprecated): https://github.com/request/request- MDN fetch: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
- axios: https://axios-http.com/docs/api_intro