Dynamics CRM Bulk Delete C# Using ExecuteMultipleRequest

To avoid calling OrganizationService.Delete multiple times. By using ExecuteMultipleRequest we can delete a bunch of records at a time. Dynamics CRM Bulk Delete C#

Max patch size is 1000. You can delete 1000 records per request

Below is sample code:

/// <summary> /// Call this method for bulk delete /// </summary> /// <param name="service">Org Service</param> /// <param name="entityReferences">Collection of EntityReferences to Delete</param> public static void BulkDelete(IOrganizationService service, DataCollection<EntityReference> entityReferences) { // Create an ExecuteMultipleRequest object. var multipleRequest = new ExecuteMultipleRequest() { // Assign settings that define execution behavior: continue on error, return responses. Settings = new ExecuteMultipleSettings() { ContinueOnError = false, ReturnResponses = true }, // Create an empty organization request collection. Requests = new OrganizationRequestCollection() }; // Add a DeleteRequest for each entity to the request collection. foreach (var entityRef in entityReferences) { DeleteRequest deleteRequest = new DeleteRequest { Target = entityRef }; multipleRequest.Requests.Add(deleteRequest); } // Execute all the requests in the request collection using a single web method call. ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest); }
Code language: PHP (php)

Leave a Reply