Managing size of AsyncOperationBase table in CRM 4.0

I found this (http://blogs.msdn.com/crm/archive/2008/07/29/managing-size-of-asyncoperationbase-table-in-crm-4-0.aspx) very interesting article about the size of your MSCRM database.
Several GB were present in ours already, mostly because of the AsyncOperationBase table.

I had the change the code to get it working on my CRM 4. I remove the part about the operationtype to delete all kind of objects, but left it for this example. Here is the result:

QueryExpression expression = new QueryExpression();
expression.EntityName = EntityName.asyncoperation.ToString();
ColumnSet cs=new ColumnSet();
cs.Attributes=new string[] { "asyncoperationid" };
expression.ColumnSet = cs;

ConditionExpression cState = new ConditionExpression();
cState.AttributeName = "statecode";
cState.Operator = ConditionOperator.Equal;
cState.Values = new object[1];
cState.Values[0] = (int)AsyncOperationState.Completed;

ConditionExpression cCompletedon = new ConditionExpression();
cCompletedon.AttributeName = "completedon";
cCompletedon.Operator = ConditionOperator.OlderThanXMonths;
cCompletedon.Values = new object[1];
cCompletedon.Values[0] = 1;

ConditionExpression cOperationtype = new ConditionExpression();
cOperationtype.AttributeName = "operationtype";
cOperationtype.Operator = ConditionOperator.NotEqual;
cOperationtype.Values = new object[1];
cOperationtype.Values[0] =10;//(int)AsyncOperationType.Workflow;

FilterExpression fe = new FilterExpression();

fe.Conditions = new ConditionExpression[] { cState, cCompletedon, cOperationtype };
expression.Criteria = fe;

Guid[] emptyRecipients = new Guid[0];
BulkDeleteRequest request = new BulkDeleteRequest();

request.JobName = "Bulk delete completed asyncoperations to free up space";
request.QuerySet = new QueryBase[] { expression };
request.ToRecipients = emptyRecipients;
request.CCRecipients = emptyRecipients;
request.SendEmailNotification = false;
request.RecurrencePattern = string.Empty;
request.StartDateTime = new CrmDateTime();
//enable this code for recurring job
/*request.StartDateTime.Value = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss");
request.RecurrencePattern = "FREQ=DAILY;";//INTERVAL=7;*/
try
{
	BulkDeleteResponse response =(BulkDeleteResponse)Service.Execute(request);
	Response.Write(string.Format("Bulk delete job id: {0}", response.JobId));
}
catch (SoapException ex)
{
	Response.Write(ex.ToString() + "
" + ex.Detail.InnerXml.ToString());
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.