Back to all resources
API
fetch
Delete

Fetch - Delete

By Jared Malan
This JavaScript code snippet performs a DELETE request to an API endpoint to remove a resource specified by its ID. The resource ID is defined by the variable resource. The API endpoint URL is constructed dynamically using the resource ID. The fetch API is used to send the DELETE request to the server.

Upon successful deletion, the server is expected to respond with a JSON object, which is then logged to the console. If there is an error during the fetch operation, such as a network issue or if the server responds with an error status, the error is caught and logged to the console using console.error.

This code does not handle specific HTTP status codes returned from the server, such as distinguishing between a successful deletion and a not-found error. It assumes that any valid JSON response is a successful operation.