Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to delete MSCRM audit logs through C# custom code

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

  • Suggested answer
    OrchidC Profile Picture
    OrchidC 200 on at
    RE: How to delete MSCRM audit logs through C# custom code

    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)

  • MSCRM learner Profile Picture
    MSCRM learner 170 on at
    RE: How to delete MSCRM audit logs through C# custom code

    Thanks for your response.

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

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: How to delete MSCRM audit logs through C# custom code

    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
    MSCRM learner 170 on at
    RE: How to delete MSCRM audit logs through C# custom code

    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
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: How to delete MSCRM audit logs through C# custom code

    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.");


  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to delete MSCRM audit logs through C# custom code

    Hi,

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

    Thanks,

    Ridhima

  • Verified answer
    Johnny Gong Profile Picture
    Johnny Gong 6,482 on at
    RE: How to delete MSCRM audit logs through C# custom code

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans