Bulk Delete with Dynamics365

No matter what project you are on, you are at one point or another going to need to delete data.

Your environment will become polluted and you going through each entity or manually queuing up your own delete jobs will not be worth your time.

To bulk delete your entire environment you can make use of the Bulk Delete API with the following lines of code.

QueryExpression bulkquery = new QueryExpression();
bulkquery.EntityName = entity.LogicalName;
bulkquery.Distinct = false;

BulkDeleteRequest request = new BulkDeleteRequest
JobName = String.Format("System Purge Requested for Entity [{0}] - All Records", bulkquery.EntityName),
QuerySet = new[] { bulkquery },
StartDateTime = DateTime.Now.ToUniversalTime(),
ToRecipients = new[] { currentUserId },
CCRecipients = new Guid[] { },
SendEmailNotification = false,
RecurrencePattern = String.Empty

ExecuteMultipleResponse response = new ExecuteMultipleResponse();
response = (ExecuteMultipleResponse)_svc.CrmService.Execute(multipleRequest);

Of critical importance is the following parameter;

StartDateTime = DateTime.Now.ToUniversalTime(),

While on-premise you will get away with Datetime.Now (presuming you are in the same Timezone) when you move to Dynamics365 your tenant will inevitably be in a different timezone.

If you do not set the start date to UniversalTime, your jobs will kick off in the “Waiting for Timer” state where they will sit until the reach the appointed time to kick off.  If you want to validate that your schedule has been set correctly, check the following field to know if you’ll be waiting longer than expected.


Post A Reply

%d bloggers like this: