Lightning web component deleteRecord function

Let us discuss here how to use lightning/uiRecordApi modules. This module includes wire adapters to record data and get default values to create records. It also includes JavaScript APIs to create, delete, update, and refresh records. one of the most of important function is deleteRecord that will be used to delete a record in the database without using apex class.

Syntax 

  • recordId—(Required) The ID of the record to delete.
Create a lightning web component
create a lightning web component using the flowing sfdx command

Use this markup in deleterecord.html

Use this deleterecord.js code

 

Use this deleterecord.js-meta.xml file

 

Push changes to scratch org.

Push changes to scratch using this command and add this component to record page using lighting app builder

Add the Delete Record component to Salesforce record page using app builder and you can able to see the delete button as shown below.

 

 

Understand the code 

Import deleteRecord function from lightning/uiRecordApi modules .

invoke the deleteRecord function on the button click as shown below. This method will show the Toast message once the record is deleted or fail to delete.