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

Notifications

Announcements

No record found.

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

I have the same question (0)
  • 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,256 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans