useDelete is a modified version of TanStack Query’s useMutation for delete mutations.
It uses the deleteOne method from the dataProvider and supports optimistic updates, pessimistic updates, and undoable mutations.
Usage
Parameters
Parameters are passed to themutate/mutateAsync functions.
Resource name for API data interactions.
ID of the record to delete. Can be a
string or number.Determines when mutations are executed. Defaults to the global mutation mode.
pessimistic: Deletion is applied after the server respondsoptimistic: Deletion is applied immediately, rolled back on errorundoable: Deletion is applied immediately with an option to undo
Duration in milliseconds to wait before executing the mutation when
mutationMode is undoable.Callback that provides a function to cancel the mutation when
mutationMode is undoable.Meta data for the dataProvider. Can be used to pass additional parameters to data provider methods.
If there is more than one
dataProvider, you should specify which one to use.Specify which queries should be invalidated after successful mutation. Options:
'list', 'many', 'detail', 'resourceAll', 'all'.Additional values to pass to the delete mutation (rarely used).
Success notification configuration. Set to
false to disable.Error notification configuration. Set to
false to disable.Hook Options
TanStack Query’s
useMutation options (excluding mutationFn, onMutate, onSuccess, onError, onSettled).Configuration for loading overtime behavior.
Return Values
Function to trigger the mutation.
Async version that returns a promise.
TanStack Query’s
useMutation return object.Loading overtime information.
Examples
Basic Usage
With Confirmation
With Async/Await
Optimistic Deletion
Undoable Deletion
With Loading State
With Success Callback
Disable Notifications
Custom Notifications
Custom Invalidation
With Meta Data
Soft Delete
Type Parameters
TData- Result data type of the mutation. ExtendsBaseRecord.TError- Custom error type that extendsHttpError.TVariables- Type of additional variables/payload for the mutation.
FAQ
What's the difference between mutation modes?
What's the difference between mutation modes?
- Pessimistic: Removes from UI only after server confirms deletion (safest)
- Optimistic: Removes from UI immediately, restores on error (best UX)
- Undoable: Removes from UI immediately with option to cancel (Gmail-style)
How do I implement soft delete instead of hard delete?
How do I implement soft delete instead of hard delete?
Use
useUpdate instead of useDelete to mark records as deleted:Can I pass additional data with the delete request?
Can I pass additional data with the delete request?
Yes, use the
values parameter:How do I show a confirmation dialog before deleting?
How do I show a confirmation dialog before deleting?
Use Refine’s
useModalForm or wrap the mutate call:What queries are invalidated by default?
What queries are invalidated by default?
By default,
useDelete invalidates list and many queries for the resource. Detail queries for the specific ID are removed from the cache.