Skip to main content
Version: 4.xx.xx
Swizzle Ready

Delete

<DeleteButton> uses Ant Design's <Button> and <Popconfirm> components.

When you try to delete something, a pop-up shows up and asks for confirmation. When confirmed it executes the useDelete method provided by your dataProvider.

Good to know:

You can swizzle this component to customize it with the Refine CLI

Usage

localhost:3000
import {
List,
useTable,
DeleteButton,
} from "@refinedev/antd";
import { Table } from "antd";

const PostList: React.FC = () => {
const { tableProps } = useTable<IPost>();

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" width="50%" />
<Table.Column<IPost>
title="Actions"
dataIndex="actions"
key="actions"
render={(_, record) => (
<DeleteButton size="small" recordItemId={record.id} />
)}
width="50%"
/>
</Table>
</List>
);
};

interface IPost {
id: number;
title: string;
}

Properties

recordItemId

recordItemId allows us to manage which record will be deleted. By default, the recordItemId is inferred from the route params.

localhost:3000
import { DeleteButton } from "@refinedev/antd";

const MyEditComponent = () => {
return (
<DeleteButton
resource="posts"
recordItemId="123"
/>
);
};

Clicking the button will trigger the useDelete method and then the record whose resource is "posts" and whose id is "123" will be deleted.

resource

resource allows us to manage which resource's record is going to be deleted. By default, the resource is inferred from the route params.

localhost:3000
import { DeleteButton } from "@refinedev/antd";

const MyDeleteComponent = () => {
return <DeleteButton resource="categories" recordItemId="123" />;
};

Clicking the button will trigger the useDelete method and then the record whose resource is "categories" and whose id is "2" will be deleted.

If you have multiple resources with the same name, you can pass the identifier instead of the name of the resource. It will only be used as the main matching key for the resource, data provider methods will still work with the name of the resource defined in the <Refine/> component.

For more information, refer to the identifier section of the <Refine/> component documentation

onSuccess

onSuccess can be used if you want to do something based on the results returned after the delete request.

For example, let's console.log after deletion:

localhost:3000
import { DeleteButton } from "@refinedev/antd";

const MyDeleteComponent = () => {
return (
<DeleteButton
resourceNameOrRouteName="posts"
recordItemId="1"
onSuccess={(value) => {
console.log(value);
}}
/>
);
};

mutationMode

Determines which mode mutation will have while executing <DeleteButton>.

For more information, refer to the mutation mode documentation

import { List, DeleteButton, useTable } from "@refinedev/antd";
import { Table } from "antd";

export const PostList: React.FC = () => {
const { tableProps } = useTable<IPost>();

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column<IPost>
title="Actions"
dataIndex="actions"
render={(_, record) => (
<DeleteButton
size="small"
recordItemId={record.id}
mutationMode="undoable"
/>
)}
/>
</Table>
</List>
);
};

hideText

It is used to show and not show the text of the button. When true, only the button icon is visible.

localhost:3000
import { DeleteButton } from "@refinedev/antd";

const MyDeleteComponent = () => {
return (
<DeleteButton
recordItemId="123"
hideText={true}
/>
);
};

accessControl

This prop can be used to skip access control check with its enabled property or to hide the button when the user does not have the permission to access the resource with hideIfUnauthorized property. This is relevant only when an accessControlProvider is provided to <Refine/>

import { DeleteButton } from "@refinedev/antd";

export const MyListComponent = () => {
return (
<DeleteButton accessControl={{ enabled: true, hideIfUnauthorized: true }} />
);
};

resourceNameOrRouteName
deprecated

Use resource prop instead.

How to override confirm texts?

You can change the text that appears when you confirm a transaction with the confirmTitle prop, as well as what the 'ok' and 'cancel' buttons' text look like with the confirmOkText and confirmCancelText props.

localhost:3000
import { DeleteButton } from "@refinedev/antd";

const MyDeleteComponent = () => {
return (
<DeleteButton
recordItemId="123"
confirmTitle="Title"
confirmOkText="Ok Text"
confirmCancelText="Delete Text"
/>
);
};

API Reference

Properties

PropertyTypeDescriptionDefault
hideText

boolean

Whether should hide the text and show only the icon or not.

false

resourceNameOrRouteName

Please use resource instead.

string

Resource name for API data interactions

Reads :resource from the URL

resource

string

Resource name for API data interactions. identifier of the resource can be used instead of the name of the resource.

Inferred resource name from the route

accessControl

{ enabled?: boolean; hideIfUnauthorized?: boolean; }

undefined

Access Control configuration for the button

{ enabled: true }

recordItemId

Data item identifier for the actions with the API

Reads :id from the URL

successNotification

false

OpenNotificationParams

((data?: unknown, values?: unknown, resource?: string) => false

OpenNotificationParams)

undefined

Success notification configuration to be displayed when the mutation is successful.

'"There was an error creating resource (status code: statusCode)" or "Error when updating resource (status code: statusCode)"'

errorNotification

false

OpenNotificationParams

((error?: unknown, values?: unknown, resource?: string) => false

OpenNotificationParams)

undefined

Error notification configuration to be displayed when the mutation fails.

'"There was an error creating resource (status code: statusCode)" or "Error when updating resource (status code: statusCode)"'

onSuccess

(value: DeleteOneResponse) => void

Callback function to be called after the delete action is successful

mutationMode

"pessimistic" | "optimistic" | "undoable"

Mutation mode for the delete action

mutationMode setting from the Refine component

meta

MetaQuery

Additional meta data to pass to the delete mutation from the data provider

metaData

metaData is deprecated with refine@4, refine will pass meta instead, however, we still support metaData for backward compatibility.

MetaQuery

Additional meta data to pass to the delete mutation from the data provider

dataProviderName

string

Target data provider name for API call to be made

"default"

confirmTitle

string

Text to be displayed in the confirmation popup

"Are you sure?" or "buttons.confirm" from the i18n provider

confirmOkText

string

Confirmation button text to be displayed in the confirmation popup

"Delete" or "buttons.delete" from the i18n provider

confirmCancelText

string

Cancel button text to be displayed in the confirmation popup

"Cancel" or "buttons.cancel" from the i18n provider

invalidates

(keyof IQueryKeys)[]

Query keys to be invalidated after the delete action is successful

["list", "many"]

External Props:

It also accepts all props of Ant Design Button.