In REST APIs, it is customary to make a DELETE
request to a resource's URI (for example, /v1/publishers/{publisher}/books/{book}
) in order to delete that resource.
Resource-oriented design (AIP-121) honors this pattern through the Delete
method. These RPCs accept the URI representing that resource and usually return an empty response.
APIs should generally provide a delete method for resources unless it is not valuable for users to do so.
Delete methods are specified using the following pattern:
rpc DeleteBook(DeleteBookRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
delete: "/v1/{name=publishers/*/books/*}"
};
option (google.api.method_signature) = "name";
}
Delete
. The remainder of the RPC name should be the singular form of the resource's message name.Request
suffix.google.protobuf.Empty
.
google.longrunning.Operation
which resolves to the correct response.DELETE
.name
.name
field should be the only variable in the URI path. All remaining parameters should map to URI query parameters.body
key in the google.api.http
annotation.google.api.method_signature
annotation, with a value of "name"
. If an etag or force field are used, they may be included in the signature.FAILED_PRECONDITION
error if child resources are present. See guidance on Cascading Delete if forcing deletion of parent and child resources is necessary.
The Delete method should succeed if and only if a resource was present and was successfully deleted. If the resource did not exist, the method should send a NOT_FOUND
error.
Delete methods implement a common request message pattern:
message DeleteBookRequest {
// The name of the book to delete.
// Format: publishers/{publisher}/books/{book}
string name = 1 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference) = {
type: "library.googleapis.com/Book"
}];
}
name
field must be included. It should be called name
.
Note: This material was moved into its own document to provide a more comprehensive treatment: AIP-164.
Long-running deleteSome resources take longer to delete a resource than is reasonable for a regular API request. In this situation, the API should use a long-running operation instead:
rpc DeleteBook(DeleteBookRequest) returns (google.longrunning.Operation) {
option (google.api.http) = {
delete: "/v1/{name=publishers/*/books/*}"
};
option (google.longrunning.operation_info) = {
response_type: "google.protobuf.Empty"
metadata_type: "OperationMetadata"
};
}
google.protobuf.Empty
for most Delete RPCs, or the resource itself for soft delete (AIP-164).response_type
and metadata_type
fields must be specified (even if they are google.protobuf.Empty
).Sometimes, it may be necessary for users to be able to delete a resource as well as all applicable child resources. However, since deletion is usually permanent, it is also important that users not do so accidentally, as reconstructing wiped-out child resources may be quite difficult.
If an API allows deletion of a resource that may have child resources, the API should provide a bool force
field on the request, which the user sets to explicitly opt in to a cascading delete.
message DeletePublisherRequest {
// The name of the publisher to delete.
// Format: publishers/{publisher}
string name = 1 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference) = {
type: "library.googleapis.com/Publisher"
}];
// If set to true, any books from this publisher will also be deleted.
// (Otherwise, the request will only work if the publisher has no books.)
bool force = 2;
}
The API must fail with a FAILED_PRECONDITION
error if the force
field is false
(or unset) and child resources are present.
Sometimes, it may be necessary for users to ensure that no changes have been made to a resource that is being deleted. If a resource provides an etag, the delete request may accept the etag (as either required or optional):
message DeleteBookRequest {
// The name of the book to delete.
// Format: publishers/{publisher}/books/{book}
string name = 1 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference) = {
type: "library.googleapis.com/Book"
}];
// Optional. The etag of the book.
// If this is provided, it must match the server's etag.
string etag = 2;
}
If the etag is provided and does not match the server-computed etag, the request must fail with a ABORTED
error code.
Note: Declarative-friendly resources (AIP-128) must provide the etag
field for Delete requests.
If the service uses client-assigned resource names, Delete
methods may expose a bool allow_missing
field, which will cause the method to succeed in the event that the user attempts to delete a resource that is not present (in which case the request is a no-op):
message DeleteBookRequest {
// The book to delete.
// Format: publishers/{publisher}/books/{book}
string name = 1 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
// If set to true, and the book is not found, the request will succeed
// but no action will be taken on the server
bool allow_missing = 2;
}
More specifically, the allow_missing
flag triggers the following behavior:
etag
field is ignored.Note: Declarative-friendly resources (AIP-128) should expose the bool allow_missing
field.
If the user does not have permission to access the resource, regardless of whether or not it exists, the service must error with PERMISSION_DENIED
(HTTP 403). Permission must be checked prior to checking if the resource exists.
If the user does have proper permission, but the requested resource does not exist, the service must error with NOT_FOUND
(HTTP 404) unless allow_missing
is set to true
.
force
field.FAILED_PRECONDITION
to ABORTED
making it consistent with change to AIP-154 & AIP-134 on 2021-03-05.Get
of soft-deleted resources.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.5