Home > Extensions > API-SDK > Bulk Delete System Jobs

CRM 4.0has an endless amounts of system job logs. We perform automatic actions on a lot of entities, but we don’t need the log of those automatic actions beyond the end of that working day if it was Canceled or Completed Successfully. Unfortunately, setting up a bulk delete task for system job logs is not as easy as setting up other bulk delete jobs. I modified the code from Sean McNellis’ blog entry on leveraging bulk delete jobs to manage system job log records.

Below is my code (which is a modified version of the runBulkDelete from the Microsoft Bulk Delete Sample for deleting successfully completed or canceled jobs:

        static void runBulkDeleteAsyncoperations(CrmService service)
        {
            // Create a query expression using the QueryExpressionHelper
            QueryExpressionHelper expression = new QueryExpressionHelper("asyncoperation");
            expression.Columns.AddColumn("asyncoperationid");
            expression.Criteria.Conditions.AddCondition("statecode",
                        ConditionOperator.Equal,
                        (int)AsyncOperationState.Completed);
            expression.Criteria.Conditions.AddCondition("statuscode",
                        ConditionOperator.Equal,
                        new object[]{(int)AsyncOperationStatus.Canceled,(int)AsyncOperationStatus.Succeeded});
            Guid[] emptyRecipients = new Guid[0];
            BulkDeleteRequest request = new BulkDeleteRequest();
            request.JobName = "Bulk Delete Successfully Completed or Canceled Asyncoperations and Workflow Logs";
            request.QuerySet = new QueryBase[] { expression.Query };
            request.ToRecipients = emptyRecipients;
            request.CCRecipients = emptyRecipients;
            request.SendEmailNotification = false;
            request.RecurrencePattern = "FREQ=DAILY;INTERVAL=1;";
            request.StartDateTime = CrmDateTime.Now;

            BulkDeleteResponse response = (BulkDeleteResponse)service.Execute(request);

            Console.WriteLine("Bulk delete job with id: {0} has been created", response.JobId);
        }

Their code examples use a month time span, and if you are trying to delete everything older than 3 days, then you need to build an executable and schedule it to run daily to create the bulk deletion job. You will also need the following filter to be added to the code, and set the RecurrencePattern equal to string.empty.

expression.Criteria.Conditions.AddCondition("completedon", ConditionOperator.OnOrBefore, DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd"));

If by some chance you are curious about creating Bulk Delete Jobs in general, check out msdn blog entry.

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2011 MSCRM Blogger

This site does not necessarily represent the views of Microsoft. The material posted on this blog are provided AS-IS, and you are to use the content at your own risk. Some of the code and information on this site may use unsupported methods. If you experience any problems using the information provided through this website, please post a comment and we will do our best to assist you, however, we can in no way guarantee support or resolution.

Microsoft Dynamics®, Silverlight®, Visual Studio®, and the corresponding logos are a registered trademarks owned by Microsoft Corporation. MSCRM Blogger has made every effort to supply trademark information about company names, products, and services mentioned on this blog. All third party trademarks are the property of their respective owners. Any rights not expressly granted herein are reserved.