web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to delete MSCRM audit logs through C# custom code

(0) ShareShare
ReportReport
Posted on by 170

Hi,

How can I delete MSCRM audit log or audit log history through C# custom code and what user role will be required for doing that?

*This post is locked for comments

  • Verified answer
    Johnny Gong Profile Picture
    6,482 Moderator on at

    Hi MSCRM Developer,

    You could enable ‘Delete Audit Record Change history’ role to control user delete Audit logs.

    [View:https://www.inogic.com/blog/2018/05/deleting-audit-log-in-dynamics-365/amp/]

    In addition, there is a DeleteAuditDataRequest in the SDK:

    [View:https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg327533(v=crm.8)]

    Overall Retrieve and delete the history of audited data changes instruction:

    [View:https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/retrieve-and-delete-the-history-of-audited-data-changes]

    As I know, you can only delete all audit log records up until a specific date using that request.

    Here’s a thread for your reference:

    [View:https://community.dynamics.com/crm/f/117/t/236432]

    Hope the above could help.

    Regards

    Johnny

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    You can use DeleteAuditDataRequest  class and for privileges you can check docs.microsoft.com/.../gg334799%28v%3dcrm.8%29

    Thanks,

    Ridhima

  • Suggested answer
    Sreevalli Profile Picture
    3,264 on at

    Hi,

    Sample code for quick help,

      // Get the list of audit partitions.
    RetrieveAuditPartitionListResponse partitionRequest =
        (RetrieveAuditPartitionListResponse)_service.Execute(new RetrieveAuditPartitionListRequest());
    AuditPartitionDetailCollection partitions = partitionRequest.AuditPartitionDetailCollection;
    
    // Create a delete request with an end date earlier than possible.
    DeleteAuditDataRequest 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))
        {
           _service.Execute(deleteRequest);
            Console.WriteLine("Audit records have been deleted.");
        }
        else
        Console.WriteLine("There were no audit records that could be deleted.");


  • MSCRM learner Profile Picture
    170 on at

    Hi,

    Thanks! Now, I am able to delete bulk records of my last audit partition but it restricts me to delete current or running quarter or partition records.

    How can i delete particular records using DeleteRecordChangeHistoryRequest. can you please provide me the C# code using DeleteRecordChangeHistoryRequest?

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    You can use the Dynamics Workflow Tools utility to delete the record history. There is a new step added recently.

    Check this: github.com/.../DeleteRecordAuditHistory.md

    Hope this helps.

  • MSCRM learner Profile Picture
    170 on at

    Thanks for your response.

    But I want to achieve this through C# custom code using SDK.

  • Suggested answer
    OrchidC Profile Picture
    200 on at

    Hi,

    bump into this, I was able to execute this function with this.

    var DeleteAudit = new DeleteRecordChangeHistoryRequest();

    //-- Set entity to delete - workaround as CRM does not allow to delete Audit record directly

    DeleteAudit.Target = (EntityReference)record.Attributes["objectid"]; //this is a contact record

    if(EntityLogReference.Id != Guid.Empty)

    {

    _service.Execute(DeleteAudit); //delete all audit history

                   }

    This code runs in a loop of all Audit records and I am filtering it with specific entity w/c is the contact.

    Prerequisite reference Microsoft.Crm.Sdk.Proxy (v9.0)

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
TNJ_CRM_Team Profile Picture

TNJ_CRM_Team 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans