This document describes how to delete a trip. 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).

## Trip deletion basics

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

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

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

- `DeleteTrip()` method: [gRPC](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.TripService) or [REST](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rest/v1/providers.trips/delete.md.txt)
- `DeleteTripRequest` message: [gRPC](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#deletetriprequest) 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 trip after it has been inactive for a period of time. See [Life of a trip](https://developers.google.com/maps/documentation/mobility/fleet-engine/journeys/trips.md.txt#life_of_a_trip).

### Example: delete trip

The following example demonstrates how to delete a trip in Fleet Engine.

```java
static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_ID = "trip-8241890";

String tripName = "providers/" + PROJECT_ID + "/trips/" + TRIP_ID;

TripServiceBlockingStub tripService = TripServiceGrpc.newBlockingStub(channel);

// Delete trip request.
DeleteTripRequest deleteTripRequest = DeleteTripRequest.newBuilder()
    .setName(tripName)
    .build();

// Error handling.
try {
  tripService.deleteTrip(deleteTripRequest);
} catch (StatusRuntimeException e) {
  Status s = e.getStatus();
  switch (s.getCode()) {
    case NOT_FOUND:            // The trip doesn't exist.
      break;
    case FAILED_PRECONDITION:  // Trip is active and assigned to a vehicle.
      break;
    case PERMISSION_DENIED:
      break;
  }
  return;
}
```

## Handle errors

When deleting a trip, you might encounter a `FAILED_PRECONDITION` error,
in which case the trip is active and assigned to a vehicle.   

To proceed with the deletion, call `UpdateTrip` and update the `trip_status`
to `COMPLETE`/`CANCELED`.

## What's next

- [Create multi-destination trips](https://developers.google.com/maps/documentation/mobility/fleet-engine/journeys/trips/multi-destination.md.txt)