useDeleteMany
useDeleteMany
is an extended version of TanStack Query's useMutation
. It supports all the features of useMutation
and adds some extra features.
- It uses the
deleteMany
method as the mutation function from thedataProvider
which is passed to<Refine>
.
It is useful when you want to delete multiple records at once.
If your data provider does not have a deleteMany
method, useDeleteMany
will use the deleteOne
method instead. It is not recommended, because it will make requests one by one for each id. It is better to implement the deleteMany
method in the data provider.
Basic Usage
The useDeleteMany
hook returns many useful properties and methods. One of them is the mutate
method which expects resource
and ids
as parameters. These parameters will be passed to the deleteMany
method from the dataProvider
as parameters.
import { useDeleteMany } from "@refinedev/core";
const { mutate } = useDeleteMany();
mutate({
resource: "products",
ids: [1, 2, 3],
});
Realtime Updates
This feature is only available if you use a Live Provider.
When the useDeleteMany
mutation runs successfully, it will call the publish
method from liveProvider
with some parameters such as channel
, type
etc. It is useful when you want to publish the changes to the subscribers on the client side.
Refer to the liveProvider
documentation for more information →
Invalidating Queries
When the useDeleteMany
mutation runs successfully, by default it will invalidate the following queries from the current resource
: "list"
and "many"
. That means, if you use useList
or useMany
hooks on the same page, they will refetch the data after the mutation is completed. You can change this behavior by passing invalidates
prop.
Refer to the query invalidation documentation for more information →
Properties
mutationOptions
mutationOptions
is used to pass options to the useMutation
hook. It is useful when you want to pass additional options to the useMutation
hook.
Refer to the useMutation
documentation for more information →
useDeleteMany({
mutationOptions: {
retry: 3,
},
});
mutationOptions
does not support onSuccess
and onError
props because they override the default onSuccess
and onError
functions. If you want to use these props, you can pass them to mutate functions like this:
const { mutate } = useDeleteMany();
mutate(
{
resource: "products",
ids: [1, 2, 3],
},
{
onError: (error, variables, context) => {
// An error occurred!
},
onSuccess: (data, variables, context) => {
// Let's celebrate!
},
},
);
Mutation Parameters
resource
required
It will be passed to the deleteMany
method from the dataProvider
as a parameter. The parameter is usually used as an API endpoint path. It all depends on how to handle the resource
in the deleteMany
method. See the creating a data provider section for an example of how resources are handled.
const { mutate } = useDeleteMany();
mutate({
resource: "categories",
});
ids
required
It will be passed to the deleteMany
method from the dataProvider
as a parameter. It is used to determine which records will be deleted.
const { mutate } = useDeleteMany();
mutate({
ids: [1, 2, 3],
});
mutationMode
Mutation mode determines which mode the mutation runs with. Mutations can run under three different modes: pessimistic
, optimistic
, and undoable
. The default mode is pessimistic
.
Each mode corresponds to a different type of user experience.
Refer to the mutation mode documentation for more information →
const { mutate } = useDeleteMany();
mutate({
mutationMode: "undoable",
});
undoableTimeout
When mutationMode
is set to undoable
, undoableTimeout
is used to determine the duration to wait before executing the mutation. The default value is 5000
milliseconds.
const { mutate } = useDeleteMany();
mutate({
mutationMode: "undoable",
undoableTimeout: 10000,
});
onCancel
When mutationMode
is set to undoable
, onCancel
is used to determine what to do when the user cancels the mutation.
const { mutate } = useDeleteMany();
mutate({
mutationMode: "undoable",
onCancel: (cancelMutation) => {
cancelMutation();
// you can do something else here
},
});
successNotification
NotificationProvider
is required for this prop to work.
After data is fetched successfully, useDeleteMany
can call open
function from NotificationProvider
to show a success notification. With this prop, you can customize the success notification.
const { mutate } = useDeleteMany();
mutate({
successNotification: (data, ids, resource) => {
return {
message: `${data.title} Successfully fetched.`,
description: "Success with no errors",
type: "success",
};
},
});
errorNotification
NotificationProvider
is required for this prop to work.
After data fetching is failed, useDeleteMany
will call open
function from NotificationProvider
to show an error notification. With this prop, you can customize the error notification.
const { mutate } = useDeleteMany();
mutate({
errorNotification: (data, ids, resource) => {
return {
message: `Something went wrong when getting ${data.id}`,
description: "Error",
type: "error",
};
},
});
meta
meta
is used following two purposes:
- To pass additional information to data provider methods.
- Generate GraphQL queries using plain JavaScript Objects (JSON). Please refer GraphQL for more information.
In the following example, we pass the headers
property in the meta
object to the deleteMany
method. With similar logic, you can pass any properties to specifically handle the data provider methods.
const { mutate } = useDeleteMany();
mutate({
meta: {
headers: { "x-meta-data": "true" },
},
});
const myDataProvider = {
//...
deleteMany: async ({
resource,
ids,
meta,
}) => {
const headers = meta?.headers ?? {};
const url = `${apiUrl}/${resource}`;
//...
//...
const { data } = await httpClient.delete(url, { ids }, { headers });
return {
data,
};
},
//...
};
dataProviderName
If there is more than one dataProvider
, you can specify which one to use by passing the dataProviderName
prop. It is useful when you have a different data provider for different resources.
const { mutate } = useDeleteMany();
mutate({
dataProviderName: "second-data-provider",
});
invalidates
invalidates
is used to specify which queries should be invalidated after the mutation is completed.
By default, it invalidates the following queries from the current resource
: "list"
and "many"
. That means, if you use useList
or useMany
hooks on the same page, they will refetch the data after the mutation is completed.
const { mutate } = useDeleteMany();
mutate({
invalidates: ["list", "many", "detail"],
});
Return Values
Returns an object with TanStack Query's useMutation
return values.
Refer to the useMutation
documentation for more information →
API
Mutation Parameters
Property | Description | Type | Default |
---|---|---|---|
resource Required | Resource name for API data interactions | string | |
ids Required | ids for mutation function | BaseKey[] | |
mutationMode | Determines when mutations are executed | "pessimistic | "optimistic | "undoable" | "pessimistic" * |
undoableTimeout | Duration to wait before executing the mutation when mutationMode = "undoable" | number | 5000ms * |
onCancel | Callback that runs when undo button is clicked on mutationMode = "undoable" | (cancelMutation: () => void) => void | |
successNotification | Successful Mutation notification | SuccessErrorNotification | "Successfully deleted resource " |
errorNotification | Unsuccessful Mutation notification | SuccessErrorNotification | "Error when updating resource (status code: statusCode )" |
meta | Meta data query for dataProvider | MetaDataQuery | |
dataProviderName | If there is more than one dataProvider , you should use the dataProviderName that you will use. | string | default |
invalidates | You can use it to manage the invalidations that will occur at the end of the mutation. | all , resourceAll , list , many , detail , false | ["list", "many"] |
*
: These props have default values inRefineContext
and can also be set on<Refine>
component.useDeleteMany
will use what is passed to<Refine>
as default but a local value will override it.
Type Parameters
Property | Desription | Type | Default |
---|---|---|---|
TData | Result data of the mutation. Extends BaseRecord | BaseRecord | BaseRecord |
TError | Custom error object that extends HttpError | HttpError | HttpError |
TVariables | Values for mutation function | {} | {} |
Return value
Description | Type |
---|---|
Result of the TanStack Query's useMutation | UseMutationResult< { data: TData }, TError, { resource: string; ids: BaseKey[]; }, DeleteContext> * |