{"id":14259,"date":"2007-02-02T10:10:00","date_gmt":"2007-02-02T10:10:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/crm\/2007\/02\/02\/performing-a-crm-data-bulk-delete\/"},"modified":"2023-05-31T15:32:18","modified_gmt":"2023-05-31T22:32:18","slug":"performing-a-crm-data-bulk-delete","status":"publish","type":"post","link":"https:\/\/www.microsoft.com\/en-us\/dynamics-365\/blog\/no-audience\/2007\/02\/02\/performing-a-crm-data-bulk-delete\/","title":{"rendered":"Performing a CRM Data Bulk Delete"},"content":{"rendered":"
We recently received a question about how to write the code to delete all records returned from a query. Here\u2019s the answer\u2026
\n
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<\/A>, which is triggered by the delete<\/A> 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#]<\/FONT> using System;<\/FONT><\/P> using System.Collections.Generic;<\/FONT><\/P> using System.Text;<\/FONT><\/P> using DeleteAllCompetitors.CrmSdk;<\/FONT><\/P> namespace DeleteAllCompetitors<\/FONT><\/P> {<\/FONT><\/P> <\/SPAN>class BulkDelete<\/FONT><\/FONT><\/P> <\/SPAN>{<\/FONT><\/FONT><\/P> <\/SPAN>static void <\/SPAN>{<\/FONT><\/FONT><\/P> <\/SPAN>\/\/ Set up the CRM Service.<\/FONT><\/FONT><\/P> <\/SPAN>CrmService service = new CrmService();<\/FONT><\/FONT><\/P> <\/SPAN>service.Credentials = <\/FONT><\/FONT><\/P> <\/SPAN> <\/SPAN>System.Net.CredentialCache.DefaultCredentials;<\/FONT><\/FONT><\/P> <\/SPAN>\/\/ Create the ColumnSet that indicates the fields to be retrieved.<\/FONT><\/FONT><\/P> <\/SPAN>ColumnSet cols = new ColumnSet();<\/FONT><\/FONT><\/P> <\/SPAN>\/\/ Set the properties of the ColumnSet.<\/FONT><\/FONT><\/P> <\/SPAN>cols.Attributes = new string[] { “competitorid” };<\/FONT><\/FONT><\/P> <\/SPAN>\/\/ Create the ConditionExpression.<\/FONT><\/FONT><\/P> <\/SPAN>ConditionExpression condition = new ConditionExpression();<\/FONT><\/FONT><\/P> <\/SPAN>\/\/ Create the query expression.<\/FONT><\/FONT><\/P> <\/SPAN>QueryExpression query = new QueryExpression();<\/FONT><\/FONT><\/P> <\/SPAN>\/\/ Set the query to retrieve accounts.<\/FONT><\/FONT><\/P> <\/SPAN>query.EntityName = EntityName.competitor.ToString();<\/FONT><\/FONT><\/P> <\/SPAN>\/\/ Create the request object.<\/FONT><\/FONT><\/P> <\/SPAN>RetrieveMultipleRequest retrieive = new RetrieveMultipleRequest();<\/FONT><\/FONT><\/P> <\/SPAN>\/\/ Set the properties of the request object.<\/FONT><\/FONT><\/P> <\/SPAN>retrieive.Query = query;<\/FONT><\/FONT><\/P> <\/SPAN>\/\/ Execute the request.<\/FONT><\/FONT><\/P> <\/SPAN>RetrieveMultipleResponse retrieived = <\/FONT><\/FONT><\/P> <\/SPAN>(RetrieveMultipleResponse)service.Execute(retrieive);<\/FONT><\/FONT><\/P> <\/SPAN>BusinessEntityCollection competitors = <\/FONT><\/FONT><\/P> <\/SPAN>retrieived.BusinessEntityCollection;<\/FONT><\/FONT><\/P> <\/SPAN>for (int i = 0; i < competitors.BusinessEntities.Length; i++)<\/FONT><\/FONT><\/P> <\/SPAN>{<\/FONT><\/FONT><\/P> <\/SPAN>competitor entity = (competitor)competitors.BusinessEntities[i];<\/FONT><\/FONT><\/P> <\/SPAN>\/\/ The EntityName indicates the EntityType <\/FONT><\/FONT><\/P> <\/SPAN>\/\/ of the object being deleted.<\/FONT><\/FONT><\/P> <\/SPAN>service.Delete(EntityName.competitor.ToString(), <\/FONT><\/FONT><\/P> <\/SPAN>entity.competitorid.Value);<\/FONT><\/FONT><\/P> <\/SPAN>}<\/FONT><\/FONT><\/P>
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n