This document describes how to delete a vehicle. It assumes you
have set up Fleet Engine. See [Set up Fleet Engine](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet.md.txt).

## Vehicle deletion basics

Your system may use Fleet Engine to delete a vehicle in the following situations:

- To perform cleanup operations while testing Fleet Engine APIs.
- To immediately delete a Vehicle that is no longer required.

To delete a vehicle, send a request using either gRPC or REST.

- `DeleteVehicle()` method: [gRPC](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.VehicleService) or [REST](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rest/v1/providers.vehicles/delete.md.txt)
- `DeleteVehicleRequest` message: [gRPC](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#deletevehiclerequest) only

Use the appropriate credentials for the service account of your project as
described in [Fleet Engine: Service account roles](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/service-accounts.md.txt).

> [!NOTE]
> **Note:** Fleet Engine automatically deletes a vehicle after it has been inactive for a period of time. See [Vehicle re-use](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/vehicles.md.txt#vehicle_re-use).

## Example: delete vehicle

### Java


The following example shows how to use the [Java gRPC library](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/client-libraries-trips.md.txt#java) to delete a
vehicle. For a complete list of supported languages, see
[Client libraries for on-demand trip services](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/client-libraries-trips.md.txt).

```java
  static final String PROJECT_ID = "my-delivery-co-gcp-project";
  static final String VEHICLE_ID = "vehicle-8241890";

  String vehicleName = "providers/" + PROJECT_ID + "/vehicles/" + VEHICLE_ID;

  VehicleServiceBlockingStub vehicleService = VehicleService.newBlockingStub(channel);

  // Delete Vehicle request
  DeleteVehicleRequest deleteVehicleRequest = DeleteVehicleRequest.newBuilder()
      .setName(vehicleName)
      .build();

  try {
    vehicleService.deleteVehicle(deleteVehicleRequest);
  } catch (StatusRuntimeException e) {
    Status s = e.getStatus();
    switch (s.getCode()) {
       case NOT_FOUND:            // The vehicle doesn't exist.
         break;
       case FAILED_PRECONDITION:  // There are trip(s) that reference vehicle.
         break;
       case PERMISSION_DENIED:
         break;
    }
    return;
  }
```

### REST


The following example shows how to delete a vehicle from Fleet Engine using REST by
making a call to `DeleteVehicle`.

```bash
  # DELETE https://fleetengine.googleapis.com/v1/providers/<project_id>/vehicles/<vehicleId>
  # Set JWT, PROJECT_ID, and VEHICLE_ID in the local environment
  curl -X DELETE -H "Authorization: Bearer ${JWT}" \
    "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/vehicles/${VEHICLE_ID}"
```

If the delete operation is successful, the API returns an empty response.

## Handle errors

When deleting a vehicle, you might encounter a `FAILED_PRECONDITION`
error, in which case there are trip(s) that reference the vehicle.
To proceed with the deletion:

1. Call `SearchTrips` to find trip(s) that reference the Vehicle.
2. Call `DeleteTrip` to delete each of the found trip.

> [!WARNING]
> **Warning:** Ensure that the found trip(s) are no longer required. Once deleted, trip(s) cannot be recovered.

## What's next

- [Create an on-demand trip](https://developers.google.com/maps/documentation/mobility/fleet-engine/journeys/trips.md.txt)