This document describes how to delete a task. It assumes you have set up Fleet Engine. See Set up Fleet Engine .
Task deletion basics
Your system may use Fleet Engine to delete a task in the following situations:
- To perform cleanup operations while testing Fleet Engine APIs.
- To immediately delete a Task that is no longer required.
To delete a task, 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 task
The following example demonstrates how to delete a task in Fleet Engine.
static
final
String
PROJECT_ID
=
"my-delivery-co-gcp-project"
;
static
final
String
TASK_ID
=
"task-8241890"
;
String
taskName
=
"providers/"
+
PROJECT_ID
+
"/tasks/"
+
TASK_ID
;
DeliveryServiceBlockingStub
deliveryService
=
DeliveryServiceGrpc
.
newBlockingStub
(
channel
);
// Delete task request.
DeleteTaskRequest
deleteTaskRequest
=
DeleteTaskRequest
.
newBuilder
()
.
setName
(
taskName
)
.
build
();
// Error handling.
try
{
deliveryService
.
deleteTask
(
deleteTaskRequest
);
}
catch
(
StatusRuntimeException
e
)
{
Status
s
=
e
.
getStatus
();
switch
(
s
.
getCode
())
{
case
NOT_FOUND
:
// The task doesn't exist.
break
;
case
FAILED_PRECONDITION
:
// Task is active and assigned to a delivery vehicle.
break
;
case
PERMISSION_DENIED
:
break
;
}
return
;
}
Handle errors
When deleting a task, you might encounter a FAILED_PRECONDITION
error, in which case the task is active and assigned to a
delivery vehicle.
To proceed with the deletion, close the task
.

