Skip to main content
DELETE
/
api
/
image
/
:id
Delete Image
curl --request DELETE \
  --url https://api.example.com/api/image/:id
Delete a cat image from the Cat Data API. This endpoint permanently removes both the image file from the filesystem and its database record.

Authentication

This endpoint requires Auth0 JWT authentication. Include the JWT token in the Authorization header:
Authorization: Bearer <your-token>

Request

id
number
required
The unique identifier of the image to delete.

Response

Success Response (204)

Returns an empty response body with HTTP status code 204 No Content, indicating the image was successfully deleted.
HTTP/1.1 204 No Content

Error Responses

401 Unauthorized

Returned when the JWT token is missing or invalid.
{
  "error": "Unauthorized"
}

404 Not Found

Returned when no image exists with the specified ID in the database.

500 Internal Server Error

Returned when:
  • The database record exists but the file cannot be found in the filesystem
  • There are permission issues preventing file deletion
  • Database deletion fails

Example Requests

cURL

curl -X DELETE https://api.example.com/api/image/42 \
  -H "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..."

JavaScript Fetch

fetch('https://api.example.com/api/image/42', {
  method: 'DELETE',
  headers: {
    'Authorization': `Bearer ${token}`
  }
})
  .then(response => {
    if (response.status === 204) {
      console.log('Image deleted successfully');
    }
  });

Python Requests

import requests

headers = {
    'Authorization': f'Bearer {token}'
}

response = requests.delete(
    'https://api.example.com/api/image/42',
    headers=headers
)

if response.status_code == 204:
    print('Image deleted successfully')

Axios (JavaScript/TypeScript)

import axios from 'axios';

try {
  await axios.delete('https://api.example.com/api/image/42', {
    headers: {
      'Authorization': `Bearer ${token}`
    }
  });
  console.log('Image deleted successfully');
} catch (error) {
  console.error('Failed to delete image:', error);
}

Implementation Details

The endpoint performs a two-step deletion process:
  1. Retrieve the image metadata: Queries the database to get the filename associated with the ID
  2. Delete the file: Removes the physical file from the images/ directory using fs.unlinkSync()
  3. Delete the database record: Removes the image record from the database
The actual implementation from src/index.ts:60-68:
app.delete('/api/image/:id', checkJwt, async (req, res) => {
  const id = Number(req.params.id);
  const fileInfo = await getImage(id);
  // delete file
  fs.unlinkSync(path.join(imagesDir, fileInfo.name));
  // delete from db
  await deleteImage(id);
  res.status(204).end();
});
The database function from src/db-functions.ts:24-26:
export async function deleteImage(id: number): Promise<void> {
  await db('images').where({ id }).del();
}

Important Notes

This operation is permanent and irreversible. Once an image is deleted:
  • The file is removed from the filesystem
  • The database record is removed
  • The image cannot be recovered
  • The deletion is synchronous and blocking (fs.unlinkSync())
  • If the file has already been manually deleted from the filesystem, the endpoint will throw an error
  • There is no soft delete or trash functionality - deletion is immediate and permanent
  • Consider implementing additional authorization checks to ensure users can only delete their own images

Build docs developers (and LLMs) love