Perform Bulk Delete
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;
try
{
// 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 retrieve = new RetrieveMultipleRequest();
// Set the properties of the request object.
retrieve.Query = query;
// Execute the request.
RetrieveMultipleResponse retrieved =
(RetrieveMultipleResponse)service.Execute(retrieve);
BusinessEntityCollection competitors =
retrieved.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);
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
Console.WriteLine(ex.Message + "." + ex.Detail.InnerText);
}
}
}
}
[Visual Basic .NET]
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports BulkDelete_VB.CrmSdk
Namespace DeleteAllCompetitors
Class BulkDelete
Shared Sub Main(ByVal args As String())
' Set up the CRM Service.
Dim service As New CrmService()
service.Credentials = _
System.Net.CredentialCache.DefaultCredentials
Try
' Create the ColumnSet that indicates
' the fields to be retrieved.
Dim cols As New ColumnSet()
' Set the properties of the ColumnSet.
cols.Attributes = New String() {"competitorid"}
' Create the ConditionExpression.
Dim condition As New ConditionExpression()
' Create the query expression.
Dim query As New QueryExpression()
' Set the query to retrieve accounts.
query.EntityName = EntityName.competitor.ToString()
' Create the request object.
Dim retrieve As New RetrieveMultipleRequest()
' Set the properties of the request object.
retrieve.Query = query
' Execute the request.
Dim retrieved As RetrieveMultipleResponse = _
DirectCast(service.Execute(retrieve), _
RetrieveMultipleResponse)
Dim competitors As BusinessEntityCollection = _
retrieved.BusinessEntityCollection
For i As Integer = 0 To competitors.BusinessEntities.Length - 1
Dim entity As competitor = _
DirectCast(competitors.BusinessEntities(i), competitor)
' The EntityName indicates the EntityType
' of the object being deleted.
service.Delete(EntityName.competitor.ToString(), _
entity.competitorid.Value)
Next
Catch ex As System.Web.Services.Protocols.SoapException
Console.WriteLine(ex.Message + "." + ex.Detail.InnerText)
End Try
End Sub
End Class
End Namespace