Skip to main content

Delete Tractor

Delete a tractor from the catalog. This endpoint requires administrator authentication.
Admin Access Required - This endpoint requires a valid JWT token with role_id: 1 (administrator).
Soft Delete Implementation - This endpoint performs a soft delete by setting the tractor’s status to inactive rather than removing it from the database. The tractor record is preserved for data integrity.

Endpoint

DELETE /api/tractors/:id

Authentication

This endpoint requires both authentication and administrator privileges:
Authorization: Bearer <jwt_token>
See the Authentication guide for details on obtaining and using JWT tokens.

Path Parameters

id
integer
required
Unique identifier of the tractor to delete. Must be a positive integer.

Response Fields

success
boolean
Indicates if the request was successful
data
object
The tractor object after deletion (with status set to inactive)

Example Request

curl -X DELETE http://localhost:4000/api/tractors/1 \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."

Example Responses

{
  "success": true,
  "data": {
    "tractor_id": 1,
    "name": "John Deere 6130M",
    "brand": "John Deere",
    "model": "6130M",
    "engine_power_hp": 130,
    "weight_kg": 5200,
    "traction_force_kn": 45.5,
    "traction_type": "4x4",
    "tire_type": "radial",
    "tire_width_mm": 540,
    "tire_diameter_mm": 1600,
    "tire_pressure_psi": 15,
    "status": "inactive"
  }
}

Soft Delete Behavior

1

Status Check

The API first verifies that the tractor exists by querying the database with the provided ID.
2

Status Update

Instead of deleting the record, the API updates the tractor’s status field to inactive.
3

Response

The updated tractor object is returned with status: "inactive", confirming the soft delete.
4

Cache Invalidation

All tractor-related and recommendation caches are automatically invalidated.
The soft delete approach preserves historical data and maintains referential integrity with other tables (e.g., calculations, recommendations) that may reference this tractor.

Implementation Details

From src/controllers/tractorController.js:294-319:
export const deleteTractor = asyncHandler(async (req, res) => {
  const id = parseInt(req.params.id, 10);

  if (Number.isNaN(id) || id <= 0) {
    return res.status(400).json({
      success: false,
      message: 'ID de tractor inválido',
    });
  }

  const existing = await Tractor.findById(id);

  if (!existing) {
    return res.status(404).json({
      success: false,
      message: 'Tractor no encontrado',
    });
  }

  // Soft delete: sets status to 'inactive'
  const updated = await Tractor.update(id, { status: 'inactive' });

  return res.json({
    success: true,
    data: updated,
  });
});

Error Responses

Returned when the tractor ID in the URL path is not a valid positive integer.Invalid ID examples:
  • Negative numbers: /api/tractors/-1
  • Zero: /api/tractors/0
  • Non-numeric: /api/tractors/abc
  • Decimals: /api/tractors/1.5
Returned when no authentication token is provided.Solution: Include a valid JWT token in the Authorization header with the Bearer scheme.
Returned when the token is malformed, expired, or invalid.Solution: Login again to obtain a fresh token.
Returned when the authenticated user is not an administrator (role_id !== 1).Solution: This endpoint is restricted to administrators only. Regular users cannot delete tractors.
Returned when no tractor exists with the specified ID.The tractor may have already been deleted or never existed in the database.
Returned when an unexpected server error occurs (e.g., database connection issues).

Idempotency

This endpoint is not idempotent in the traditional DELETE sense. Multiple DELETE requests to the same tractor ID will:
  1. First request: Return 200 OK with the updated tractor (status changed to inactive)
  2. Subsequent requests: Continue to return 200 OK since the tractor still exists but is already inactive
The tractor is never permanently removed from the database.

Reactivating Deleted Tractors

Since this is a soft delete, administrators can reactivate a deleted tractor using the Update Tractor endpoint:
curl -X PUT http://localhost:4000/api/tractors/1 \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <token>" \
  -d '{"status": "available"}'

Cache Invalidation

Deleting (deactivating) a tractor automatically invalidates:
  • All tractor list caches (*tractors*)
  • All recommendation caches (*recommendations*)
This ensures that the deleted tractor is immediately excluded from API responses.

Impact on Other Endpoints

After deletion (soft delete to inactive):

List Tractors

Inactive tractors are still returned unless filtered by status

Get Available

Inactive tractors are excluded (only shows status: 'available')

Search

Inactive tractors may appear in search results unless filtered

Recommendations

Inactive tractors should be excluded from recommendation logic

Get Tractor

View tractor details before deletion

Update Tractor

Reactivate a deleted tractor by changing status

List Tractors

View all tractors including inactive ones

Create Tractor

Add new tractors to replace deleted ones

Build docs developers (and LLMs) love