Use XRM tooling to delete data

There are two methods available in the CrmServiceClient class for deleting data in Microsoft Dataverse: DeleteEntity(String, Guid, Guid) and DeleteEntityAssociation(String, Guid, String, Guid, String, Guid).

In the Microsoft.PowerPlatform.Dataverse.Client namespace there are CRUDExtentions.DeleteEntity and CRUDExtentions.DeleteEntityAssociation methods.

Read the following important information about using a connection string in application code.

Important

Microsoft recommends that you use the most secure authentication flow available. The authentication flow described in this article requires a very high degree of trust in the application, and carries risks that are not present in other flows. You should only use this flow when other more secure flows, such as managed identities, aren't viable.

DeleteEntity

DeleteEntity is used to remove a single row of data from Dataverse. To use this method, you need to know the table schema name you wish to affect, and the GUID of the row you want to remove.

CrmServiceClient svc = new CrmServiceClient(connectionstring);
// ServiceClient svc = new ServiceClient("connectionstring");
  
// Verify that you are connected  
if (svc != null && svc.IsReady)  
{  
    // Delete the entity record  
    svc.DeleteEntity("account", <accountId>);  
}  
else  
{  
    // Display the last error.  
    Console.WriteLine("An error occurred: {0}", svc.LastCrmError);  
  
    // Display the last exception message if any.  
    Console.WriteLine(svc.LastCrmException.Message);  
    Console.WriteLine(svc.LastCrmException.Source);  
    Console.WriteLine(svc.LastCrmException.StackTrace);  
  
    return;  
}  
  

DeleteEntityAssociation

DeleteEntityAssociation removes the many-to-many association between records in tables. In this example, we will remove the association between a record in the lead and account tables.

CrmServiceClient svc = new CrmServiceClient(connectionstring);
// ServiceClient svc = new ServiceClient("connectionstring");  
  
// Verify that you are connected  
if (svc != null && svc.IsReady)  
{  
    Guid accountId = new Guid("<Account_GUID>");  
    Guid leadId = new Guid("<Lead_GUID>");  
    string accountLeadRelationshipName= "accountleads_association";   
    svc.DeleteEntityAssociation("account" , accountId, "lead" ,  leadId, accountLeadRelationshipName)  
}  
else  
{  
    // Display the last error.  
    Console.WriteLine("An error occurred: {0}", svc.LastCrmError);  
  
    // Display the last exception message if any.  
    Console.WriteLine(svc.LastCrmException.Message);  
    Console.WriteLine(svc.LastCrmException.Source);  
    Console.WriteLine(svc.LastCrmException.StackTrace);  
  
    return;  
}  
  

See also

Use XRM Tooling to connect to Dataverse
Use XRM Tooling API to execute actions in Dataverse