Introduction
One of the most common errors encountered while developing Dynamics 365 plugins is the Plugin Depth Exceeded Error. This issue usually occurs when a plugin unintentionally triggers itself repeatedly, creating a recursive loop that eventually exceeds the maximum allowed execution depth.
Before diving into plugin depth, it’s helpful to understand the overall plugin execution lifecycle. If you’re new to plugin development, check out our guide on Dynamics 365 Plugin Execution Pipeline.
A typical error message may look similar to:
This workflow job was canceled because the workflow that started it included an infinite loop. Correct the workflow logic and try again.
or
The plug-in execution has exceeded the maximum depth allowed.
Understanding why this happens and how to prevent it is essential for building scalable and reliable Dynamics 365 solutions.
What is Plugin Depth in Dynamics 365?
Plugin depth represents the number of times a plugin or custom operation has been triggered within the same execution chain.
Dynamics 365 uses depth tracking to prevent endless recursive executions that could negatively impact system performance.
The execution context contains a property called:
context.Depth
This property indicates the current execution level.
Example:
| Action | Depth |
|---|---|
| User updates Account | 1 |
| Plugin updates Account | 2 |
| Plugin triggers again | 3 |
| Plugin triggers again | 4 |
The depth continues increasing until Dynamics 365 stops execution.

Plugin depth becomes easier to understand once you’re familiar with how plugin stages execute. Our Pre Image vs Post Image in Dynamics 365 guide explains what information is available during each stage.
Why Does Dynamics 365 Limit Plugin Depth?
Imagine a plugin updating the same record that triggered it.
Without depth control:
- User updates Account.
- Plugin executes.
- Plugin updates Account again.
- Plugin executes again.
- Plugin updates Account again.
- Process repeats forever.
To prevent infinite recursion, Dynamics 365 enforces a maximum execution depth.
Most Common Cause of Plugin Depth Exceeded Error
The most common scenario occurs when a plugin updates the same entity inside its execution.
Example:
public void Execute(IServiceProvider serviceProvider)
{
var service = serviceFactory.CreateOrganizationService(context.UserId);
Entity account = new Entity("account");
account.Id = context.PrimaryEntityId;
account["description"] = "Updated by plugin";
service.Update(account);
}
If this plugin is registered on the Update message of Account, the update operation triggers the plugin again, creating an endless loop.

How to Check Current Plugin Depth
The execution context exposes the Depth property.
int currentDepth = context.Depth;
Example:
tracingService.Trace($"Current Plugin Depth: {context.Depth}");
This helps identify recursive executions during debugging.
Solution 1: Use a Depth Check
A simple safeguard is to stop execution when depth becomes greater than 1.
if (context.Depth > 1)
{
return;
}
This prevents recursive execution.
However, depth checks should not be considered the primary solution because they may hide underlying design issues.
Solution 2: Update Only When Required
Before performing an update, verify that the value actually changed.
Bad Approach:
service.Update(account);
Better Approach:
if(existingDescription != newDescription)
{
service.Update(account);
}
This significantly reduces unnecessary executions.
Solution 3: Use Filtering Attributes
When registering a plugin step, configure filtering attributes.
Example:
Instead of triggering on every Account update:
- Name
- Phone
- Address
Trigger only when:
- Credit Limit
changes.
Benefits:
- Better performance
- Fewer executions
- Reduced recursion risk
Solution 4: Use Pre-Operation Plugins
Many developers perform updates inside Post-Operation plugins.
A better approach is often to use Pre-Operation plugins.
If you’re unsure which plugin execution mode to use, read our comparison of Synchronous vs Asynchronous Plugins in Dynamics 365.
Instead of:
service.Update(entity);
Modify the Target entity directly:
target["description"] = "Updated Value";
The platform saves the change as part of the same transaction.
Benefits:
- No additional update call
- Better performance
- No recursive execution
Solution 5: Use Shared Variables
When multiple plugin steps are involved, Shared Variables can be used to track whether processing has already occurred.
Example:
context.SharedVariables["Processed"] = true;
Subsequent plugin steps can check this value before executing logic.
Real-World Scenario
Consider an Account Update plugin:
Business Requirement:
Whenever Account Name changes:
- Generate a custom code
- Store it in a field
Incorrect Implementation:
- Account Name updated.
- Plugin executes.
- Plugin updates Account again.
- Plugin re-executes.
- Depth increases.
Correct Implementation:
Modify the Target entity during Pre-Operation and avoid additional Update calls.
Result:
- Single execution
- Better performance
- No depth issues
Similar recursion issues can also occur when combining plugins and cloud flows. Understanding when to use each technology is critical. Read our Power Automate vs Dynamics 365 Plugins comparison.
Best Practices to Avoid Plugin Depth Issues
Do
✔ Use Pre-Operation when possible
✔ Configure Filtering Attributes
✔ Update only changed fields
✔ Trace execution depth during debugging
✔ Design plugins to be idempotent
Don’t
✘ Update the same record unnecessarily
✘ Use depth checks as the only solution
✘ Register plugins on all attribute changes
✘ Perform redundant Update calls
Frequently Asked Questions
What is the maximum plugin depth in Dynamics 365?
Dynamics 365 maintains execution depth limits to prevent recursive loops and excessive system resource consumption.
Is checking context.Depth > 1 enough?
No. While it prevents recursion, the preferred solution is designing plugins that do not trigger unnecessary updates.
Can workflows and Power Automate flows also increase depth?
Yes. Workflows, plugins, custom actions, and other operations participating in the same execution chain can contribute to increasing depth.
Does Plugin Trace Log show depth?
Yes. The Depth property can be logged using the tracing service and reviewed in Plugin Trace Logs.
Conclusion
The Plugin Depth Exceeded Error is usually a symptom of recursive plugin design rather than the root problem itself.
“Understanding how
context.Depthworks is essential for preventing recursive plugin execution and infinite loops in Dynamics 365.”
The most effective prevention techniques are:
- Using Pre-Operation plugins
- Avoiding unnecessary updates
- Applying filtering attributes
- Updating only changed data
- Designing efficient plugin logic
By following these practices, you can eliminate recursion issues and build high-performance Dynamics 365 solutions that scale reliably in production environments.