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 :
Customer experience | Sales, Customer Insights,...
Answered

How to Query Deleted Objects Using the Dynamics Web API?

(2) ShareShare
ReportReport
Posted on by 10

Hi everyone,

I’m working with the Dynamics Web API and am looking for a way to query deleted objects, such as opportunities. While I know that the audits entity

(api/data/v9.1/audits$filter=action+eq+3+and+objecttypecode+eq+opportunity)

can be used to query deletion objects.

I’m wondering if there are any other methods or API endpoints available to directly query or filter deleted records.

 

Thanks in advance!

I have the same question (0)
  • Verified answer
    Daivat Vartak (v-9davar) Profile Picture
    7,835 Super User 2025 Season 2 on at
    Hello DU-04040635-0,
     

    You're right, the audits entity is the primary way to track deletions in Dynamics 365 (Dataverse) via the Web API. However, there isn't a dedicated, direct API endpoint or filter to retrieve only deleted records without resorting to the audit logs.

    Here's a breakdown of why and what alternatives you have:

    Why No Direct Endpoint?

    • Logical Deletion: Dynamics 365 primarily uses "logical" deletions. When a record is "deleted," it isn't physically removed from the database immediately. Instead, its state is changed (e.g., statecode is set to 2 for inactive).
    • Performance: Maintaining an index or separate endpoint for physically deleted records would be resource-intensive, especially in large databases.
    • Audit Trail: The audit logs are designed to provide a comprehensive history of all data changes, including deletions.

    •  

    Alternatives and Considerations:

    1. Using the audits Entity (Most Common):

      • As you've already found, this is the standard approach.
      • Filtering: You'll need to filter the audits entity using action eq 3 (delete) and objecttypecode eq [entity type code] (e.g., 4 for opportunities).
      • Retrieving Object ID: The objectid field in the audits entity contains the GUID of the deleted record.

      • Limitations:

        • Can be slow for large audit tables.
        • Requires post-processing to extract the relevant data.
        • Audit logs might be purged according to retention policies. 

      • Example Web API Query:

        [your-org-url]/api/data/v9.1/audits?$filter=action eq 3 and objecttypecode eq 

         

    2. Custom Logic and a "Deleted" Flag (If You Have Control):

      • Concept:

        • If you have control over how records are "deleted" in your application, you can add a custom "Deleted" boolean field to the entity.
        • Instead of using the standard delete operation, update the record and set the "Deleted" flag to true.
        • Then, you can easily query the entity for records where "Deleted" is true. 

      • Implementation:

        • Create a custom "Deleted" boolean field.
        • Modify your application logic to set this flag instead of using the delete operation.
        • Use Web API queries to filter records based on the "Deleted" flag. 

      • Advantages:

        • Faster and more efficient querying.
        • Provides direct access to deleted records. 

      • Disadvantages:

        • Requires modifying your application logic.
        • Not applicable to records deleted using the standard Dynamics 365 interface.

        •  
         

    3. Dataverse Change Tracking (For Incremental Changes):

      • Concept:

        • Dataverse change tracking can be used to retrieve incremental changes to data, including deletions.
        • This is more suitable for synchronization scenarios than for general querying of deleted records. 

      • Limitations:

        • Requires enabling change tracking on the entity.
        • More complex to implement.
        • Primarily for retrieving changes since a specific version or timestamp. 

      • When to use:

        • When needing to know what has changed since the last sync.

        •  

    4. SQL Direct Query (If On-Premises and Supported):

      • Concept:

        • If you are using an on-premises deployment of Dynamics 365, you might be able to query the underlying SQL Server database directly. 

      • Limitations:

        • Not supported by Microsoft.
        • Can be risky and lead to data corruption.
        • Not applicable to Dynamics 365 Online.
        • The database structure could change between versions. 

      • Avoid this method if possible.


      •  

    Recommendations:

    • The audits entity is the most reliable and supported way to query deleted records using the Web API.
    • If you have control over the deletion process, consider adding a custom "Deleted" flag for more efficient querying.
    • Avoid direct SQL queries unless absolutely necessary, and only if on prem.
    • Dataverse change tracking is for synching data, not general deleted data queries.

    •  

    By using the audits entity and carefully filtering the results, you can retrieve the deleted records you need.

     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more. If you have further questions, please feel free to contact me.
     
    My response was crafted with AI assistance and tailored to provide detailed and actionable guidance for your Microsoft Dynamics 365 query.
     
    Regards,
    Daivat Vartak

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans