Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

CRM 2016 the best way to make bulk delete?

Posted on by 805

Hello,

I need to schedule a custom workflow activity that delete all entity records and populate it again from external source.

I am going to use bulk delete but as far as I know it is executed asynchronously (an delete records in a batches) by the CRM, so it is not easy to understand when it is completed (in order to be started the population process). I made some research in google and found the following solution:

        private void DeleteAllRecords(IOrganizationService _service)
        {
            BulkDeleteRequest request = new BulkDeleteRequest
            {
                JobName = "Delete All",
                ToRecipients = new Guid[] { },
                CCRecipients = new Guid[] { },
                RecurrencePattern = string.Empty,

                QuerySet = new QueryExpression[] 
                {
                    new QueryExpression { EntityName = "my_entity" }
                }
            };

            BulkDeleteResponse response = (BulkDeleteResponse)_service.Execute(request);
            Guid jobId = response.JobId;

            deleteInProcess = true;

            while (deleteInProcess)
            {
                Thread.Sleep(5000); 

                QueryExpression query = new QueryExpression { EntityName = "bulkdeleteoperation" };
                query.Criteria.AddCondition("asyncoperationid", ConditionOperator.Equal, jobId);
                query.Criteria.AddCondition("statecode", ConditionOperator.Equal, 3);
                query.Criteria.AddCondition("statuscode", ConditionOperator.Equal, 30);

                EntityCollection results = _service.RetrieveMultiple(query);
                if (results.Entities.Count > 0)
                {
                    deleteInProcess = false;
                }
            }
        }


Is there a better solution? 

Regards, 

*This post is locked for comments

  • ist Profile Picture
    ist 805 on at
    RE: CRM 2016 the best way to make bulk delete?

    I didn't manage to figure out how to use paging with ExecuteMultipleRequest.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: CRM 2016 the best way to make bulk delete?

    Hi,

    The idea is good , but may I know how many data are you are going to delete in one request ?,

    you can  try with execute multiple request  , seems this is faster than bulkdeleterequest.

    msdynamicscrmblog.wordpress.com/.../use-executemultiplerequest-for-bulk-createupdatedelete-using-c-in-dynamics-crm-2011-2

    community.dynamics.com/.../deleting-a-lot-of-records-fast

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: CRM 2016 the best way to make bulk delete?

    If your code is populating the data, you could do the deletion from your code, no need to use bulk deletion job.

    You could use the execute multiple to batch the deletes in your code

      public static void BulkDelete(IOrganizationService service, DataCollection<EntityReference> entityReferences)

      {

          var multipleRequest = new ExecuteMultipleRequest()

          {

              Settings = new ExecuteMultipleSettings()

              {

                  ContinueOnError = false,

                  ReturnResponses = true

              },

              // Create an empty organization request collection.

              Requests = new OrganizationRequestCollection()

          };

          // Add a DeleteRequest for each entity to the request collection.

          foreach (var entityRef in entityReferences)

          {

              DeleteRequest deleteRequest = new DeleteRequest { Target = entityRef };

              multipleRequest.Requests.Add(deleteRequest);

          }

          // Execute all the requests in the request collection using a single web method call.

          ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);

      }

    https://msdynamicscrmblog.wordpress.com/2013/05/10/use-executemultiplerequest-for-bulk-createupdatedelete-using-c-in-dynamics-crm-2011-2/

    If you have more than 5k records, you will have to use paging when you retrieve records

    Refer to this link to see how you can do paging - pull 5k records from CRM at a time and then use the BulkDelete method mentioned above to delete those 5k and then carry on doing until the last record

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/org-service/sample-use-queryexpression-with-a-paging-cookie

    Just wanted to add this as well : What happens if you create a workflow on Email Created/Received and check if its bulk delete completed email, then start inserting your data if you do not want to use your code to do the bulk delete, this is just an idea and its possible.

    Btw, I assume is this bulk delete is related to your question on other thread about external lookup values storing in CRM

  • ist Profile Picture
    ist 805 on at
    RE: CRM 2016 the best way to make bulk delete?

    Hi Kokulan,

    Thanks for your help.

    Your solution looks fine.

    Most probably I do not understand something.

    I need to start an entity population once the deletion is completed, that's why the emails sending is not the case.

    Ho can I achieve that?

    p.s. The solution will be running outside the working hours.

    Regards,

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: CRM 2016 the best way to make bulk delete?

    Hi 

    The code in your question does not do anything special, all it does is submits a bulk delete job and then checks every 5 seconds if the job is completed.

    But I think its an inefficient way of doing a bulk delete and getting the status of the job.

    Your plugin whether it's run sync or async, is going to affect the performance of the system - so you will have to run your above code out of office hours

    You do not have to do the above, and just receive an email once the deletion completed. Please see the following

    Steps in Creating Bulk Delete Job

    From Settings, Data Management Area, select Buil Record Deletion and create new job 

    4375.ScreenClip-_5B00_203_5D00_.png

    ScreenClip-_5B00_209_5D00_.png

    4744.ScreenClip-_5B00_206_5D00_.png

    Define your query as to which records you want to delete

    4375.ScreenClip-_5B00_203_5D00_.png

    Preview and See if you are deleting the correct set of records

    ScreenClip-_5B00_204_5D00_.png

    Select the time when you want to start the bulk deletion job and also select the user who should be sent email when deletion completed.

    2046.ScreenClip-_5B00_205_5D00_.png

    Finally, submit the job

    4744.ScreenClip-_5B00_206_5D00_.png

    If you want to manually check the job progress you can see in the system jobs area to see how many deleted so far

    5751.ScreenClip-_5B00_207_5D00_.png

    Hope this helps

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans