IOrganizationService.Delete(String, Guid) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Deletes a record.
public:
void Delete(System::String ^ entityName, Guid id);
[System.ServiceModel.FaultContract(typeof(Microsoft.Xrm.Sdk.OrganizationServiceFault))]
[System.ServiceModel.OperationContract]
public void Delete (string entityName, Guid id);
[<System.ServiceModel.FaultContract(typeof(Microsoft.Xrm.Sdk.OrganizationServiceFault))>]
[<System.ServiceModel.OperationContract>]
abstract member Delete : string * Guid -> unit
Public Sub Delete (entityName As String, id As Guid)
Parameters
- entityName
- String
The logical name of the table that is specified in the id
parameter.
- id
- Guid
The ID of the record that you want to delete.
- Attributes
Examples
The following example shows four different ways to set the parameters for the Delete(String, Guid) method depending on common ways you might reference a record that you want to delete.
-
When using a generated early-bound class like the
Account
class, you can use theEntityLogicalName
property or the Entity.LogicalName property to set theentityName
parameter. Learn more about generating early-bound classes for the SDK for .NET -
When using the late-bound Entity class, you can use the Entity.LogicalName property to set the
entityName
parameter. -
When using a reference to a record defined with the EntityReference class, you can use the EntityReference.LogicalName property to set the
entityName
parameter. -
If you only have the ID value and know the type of record, you can set the
entityName
parameter to a string value that corresponds to the EntityMetadata.LogicalName property. In this case, that value is 'account
'.
For this sample to work correctly, you must be connected to the server to get an IOrganizationService interface instance.
/// <summary>
/// Demonstrates the Delete method
/// </summary>
/// <param name="service">Authenticated IOrganizationService instance</param>
/// <param name="earlyBoundAccount">An early bound Account instance.</param>
/// <param name="lateBoundAccount">A late bound account Entity record.</param>
/// <param name="accountReference">A reference to an account record.</param>
/// <param name="accountId">The ID of an account record to delete.</param>
static void DeleteAccountsExample(IOrganizationService service,
Account earlyBoundAccount,
Entity lateBoundAccount,
EntityReference accountReference,
Guid accountId)
{
// Early bound example
service.Delete(Account.EntityLogicalName, earlyBoundAccount.Id);
// Late bound example
service.Delete(lateBoundAccount.LogicalName, lateBoundAccount.Id);
// Account reference
service.Delete(accountReference.LogicalName, accountReference.Id);
// Guid ID
service.Delete("account", accountId);
}
You can find more samples on GitHub:
Remarks
Learn how to delete table rows using the SDK for .NET.
Privileges and Access Rights
To perform this action, the caller must have Delete
privileges on the table that is specified in the entityName
parameter and Delete
access rights on the record that is specified in the id
parameter.
When a record is deleted and there are related records in table that has a relationship configured for cascading delete, all child records are also deleted. The entire deletion fails if the caller does not have the Delete
privilege or access rights for any of these related records.
Learn more about how to verify access with code.
Learn more about table relationship behavior.
Notes for Callers
Typically, you should only delete records that you entered by mistake. For some tables, you can deactivate the record by updating the statecode
attribute to 1, the Inactive state.
For more information about the exceptions that can be thrown when you call this method, see Handle exceptions in your code.
Supported tables
You can use this method to create any record of a table that supports the Delete
message. Learn more about Table support for messages and Message support for tables.