Search code examples
c#pluginsdynamics-crmdynamics-crm-online

Delete Dynamics CRM Audit Log between two date


I wanna to ask if there is any idea to delete the Audit Log in Dynamics CRM Online between Two dates


Solution

  • Documentation says:

    Delete the change history for a date range

    You can delete audit records for a date range using the DeleteAuditDataRequest request. Audit data records are deleted sequentially from the oldest to the newest. The functionality of this request is slightly different based on the edition of Microsoft SQL Server being used by your Common Data Service server. Common Data Service uses an enterprise edition of SQL Server.

    If your Common Data Service server uses SQL Server standard edition, which does not support the database partitioning feature, the DeleteAuditDataRequest request deletes all audit records created up to the end date specified in the EndDate property. If your Common Data Service server uses an Enterprise edition of SQL Server that does support partitioning, the DeleteAuditDataRequest request will delete all audit data in those partitions where the end date is before the date specified in the EndDate property. Any empty partitions are also deleted. However, neither the current (active) partition nor the audit records in that active partition can be deleted by using this request or any other request.

    New partitions are automatically created by the Common Data Service platform on a quarterly basis each year. This functionality is non-configurable and cannot be changed. You can obtain the list of partitions using the RetrieveAuditPartitionListRequest request. If the end date of any partition is later than the current date, you cannot delete that partition or any audit records in it.

    We cannot delete exactly between two dates but you can delete audit data by partition only older than some end date.

    Code sample:

    // Get the list of audit partitions.
              var partitionRequest =(RetrieveAuditPartitionListResponse)svc.Execute(new RetrieveAuditPartitionListRequest());
                AuditPartitionDetailCollection partitions = partitionRequest.AuditPartitionDetailCollection;
    
    // Create a delete request with an end date earlier than possible.
             var deleteRequest = new DeleteAuditDataRequest();
                 deleteRequest.EndDate = new DateTime(2000, 1, 1);
    
    // Check if partitions are not supported as is the case with SQL Server Standard edition.
    if (partitions.IsLogicalCollection)
    {
        // Delete all audit records created up until now.
        deleteRequest.EndDate = DateTime.Now;
    }
    
    // Otherwise, delete all partitions that are older than the current partition.
    // Hint: The partitions in the collection are returned in sorted order where the 
    // partition with the oldest end date is at index 0.
    else
    {
        for (int n = partitions.Count - 1; n >= 0; --n)
        {
            if (partitions[n].EndDate<DateTime.Now && partitions[n].EndDate>deleteRequest.EndDate)
            {
                deleteRequest.EndDate=(DateTime)partitions[n].EndDate;
                break;
            }
          }
        }
    
    // Delete the audit records.
        if (deleteRequest.EndDate != new DateTime(2000, 1, 1))
        {
           svc.Execute(deleteRequest);
            Console.WriteLine("Audit records have been deleted.");
        }
        else
        Console.WriteLine("There were no audit records that could be deleted.");
    

    In web api you can use the DeleteAuditData Action.