This document describes how to delete a vehicle. It assumes you have set up Fleet Engine. See Set up Fleet Engine.
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.
Use the appropriate credentials for the service account of your project as described in Fleet Engine: Service account roles.
Example: delete vehicle
Java
The following example shows how to use the Java gRPC library to delete a vehicle.
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
.
# 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:
- Call
SearchTrips
to find trip(s) that reference the Vehicle. - Call
DeleteTrip
to delete each of the found trip.