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.

[C#]

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 =


                System.Net.CredentialCache.DefaultCredentials;


 


            // 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 =


                (RetrieveMultipleResponse)service.Execute(retrieive);


            BusinessEntityCollection competitors =


                retrieived.BusinessEntityCollection;


 


            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.


                service.Delete(EntityName.competitor.ToString(),


                               entity.competitorid.Value);


 


            }


        }


    }


}


Inna Agranov

Comments (4)

  1. jack jones says:

    This post has a ‘using DeleteAllCompetitors.CrmSdk;’ statement that is not available to us out here in real world.

    please provide a complete sample, that is useful

    to the dummies needing this functionality.

    thanks,

    jack

  2. Inna Agranov says:

    Hi Jack,

    Please follow the instruction when you create and build your project:

    1. Open Visual Studio.

    2. Create a new C# consol application project and name it DeleteAllCompetitors.

    3. Rename Program.cs file to BulkDelete.cs

    4. Select All in the BulkdDelete.cs and hit Delete.

    5. Copy and paste the code from the blog post into the BulkDelete.cs file.

    6. In the Solution Explorer, right-click the DeleteAllCompetitors project.

    7. Click Add Web Reference.

    8. In the URL text box, type in the URL to the CrmService.asmx file, as shown in the following example:

    http://<yourCRMserver>/mscrmservices/2007/CrmService.asmx

    9. Click Go.

    10. After the Web service is found, in the Web reference name text box, change the text to CrmSdk.

    11. Click on Add Reference.

    12. Build the project.

    13. Press F5 to run the project.

    Caution: Test this solution on the test Microsoft Dynamics CRM environment which you can reload after testing, because you  are dealing with the Delete action that permanently removes the data from the system.

    Please, let me know if you have any further questions.

    Thank you,

    Inna Agranov

  3. pat says:

    this delete method does not work for entityname.queueitem

    how do delete item from a queue?

    i found detachemailfromqueue to delete emails from queue. but how to delete phonecalls, task, or anything else from a queue

  4. This is the closest thing I have found that tells me how to delete extraneous records from CRM 3.0. However, I am still a long way from knowing what to do. For example, your first instruction says "Open Visual Studio". I’m lost already. Where do I find Visual Studio?

    The second instruction tells me to create a new C# consol application project. Huh??

    You’re probably getting the point by now.

    The reason I want to do this is that our CRM is very slow, and I expect it is because of all the records in it. Most of them we no longer need. I’d like to get rid of them and see if it speeds up.

    If you are able to help me with even more detailed instructions, I would be most grateful.

    I used to do programming in BASIC and Fortran, but that’s about it, I’m afraid.

Skip to main content