3 min read

Performing a CRM Data Bulk Delete

We recently received a question about how to write the code to delete all records returned from a query. Here’s the answer…

The following code example demonstrates how to delete bulk data from the Microsoft CRM 3.0 database. If you need to delete a very large amount of data, bulk delete may require a long period of time to complete. The main reason for that is cascading, which is triggered by the delete operation. If the entity instance that you are deleting has a system or parental relationship with other entity instances, the parent and all its children entity instances are deleted according to the cascading rules.


using System;

using System.Collections.Generic;

using System.Text;

using DeleteAllCompetitors.CrmSdk;


namespace DeleteAllCompetitors


    class BulkDelete


        static void Main(string[] args)


            // Set up the CRM Service.

            CrmService service = new CrmService();

            service.Credentials =



            // Create the ColumnSet that indicates the fields to be retrieved.

            ColumnSet cols = new ColumnSet();


            // Set the properties of the ColumnSet.

            cols.Attributes = new string[] { “competitorid” };


            // Create the ConditionExpression.

            ConditionExpression condition = new ConditionExpression();


            // Create the query expression.

            QueryExpression query = new QueryExpression();


            // Set the query to retrieve accounts.

            query.EntityName = EntityName.competitor.ToString();


            // Create the request object.

            RetrieveMultipleRequest retrieive = new RetrieveMultipleRequest();


            // Set the properties of the request object.

            retrieive.Query = query;


            // Execute the request.

            RetrieveMultipleResponse retrieived =


            BusinessEntityCollection competitors =



            for (int i = 0; i < competitors.BusinessEntities.Length; i++)


                competitor entity = (competitor)competitors.BusinessEntities[i];


                // The EntityName indicates the EntityType

                // of the object being deleted.








Inna Agranov