Skip to content

[28.x] Backport TruncateAll in retention policies (#7437, #7755)#7941

Merged
Jiawen-CS merged 2 commits into
releases/28.xfrom
backport/truncate-all-28x
May 4, 2026
Merged

[28.x] Backport TruncateAll in retention policies (#7437, #7755)#7941
Jiawen-CS merged 2 commits into
releases/28.xfrom
backport/truncate-all-28x

Conversation

@Jiawen-CS

@Jiawen-CS Jiawen-CS commented May 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Backport of two retention-policy TruncateAll changes from main to releases/28.x.

Cherry-picked commits

Details

#7437 — Add Truncate all in retention policies
Introduces a controlled TruncateAll capability for selected retention-policy tables (e.g. Change Log Entry, Integration Synch. Job Errors, Error Message, Error Message Register). Truncate() is orders of magnitude faster than DeleteAll() for full-table cleanup. Filtered truncate is intentionally not supported.

#7755 — Handle indirect permissions for table truncation
When a user has only indirect delete permissions on a table (e.g. Retention Policy Log Entry), RecRef.Truncate(true) fails because it requires direct permissions. Adds an integration event so a subscriber holding the required indirect permissions performs the truncation.

  • Added OnTruncateRecordsIndirectPermissionRequired integration event to the Apply Retention Policy codeunit.
  • Added codeunit 3921 Retention Policy Logs Truncate that subscribes to the event and truncates Retention Policy Log Entry with indirect permissions.
  • Modified TruncateTableRecords in RetentionPolicySetupImpl to check effective delete permissions and raise the event when permissions are indirect.
  • Bumped the object ID range in app.json from 3920 to 3921.

Testing

  • Validates against existing retention policy tests carried over from main.
  • Both PRs were already merged and validated on main.

Work Item

Jiawen-CS and others added 2 commits May 1, 2026 13:45
### Summary 

This pull request introduces a controlled **TruncateAll capability** for
selected tables within the retention policy framework, aiming to
significantly improve cleanup performance for large datasets.

####  Background

Based on ICM analysis, most performance issues are concentrated in
retention-policy-related tables (`Change Log Entry, Integration Synch.
Job Errors, and table 700 (Error Message) and table 701 (Error Message
Register)`).

Experimental results show that:

* `Truncate()` is **orders of magnitude faster** than `DeleteAll()` for
full table deletion

However, filtered truncate does not provide consistent benefits.
Filtered truncate is intentionally **not supported** in this PR due to
both performance and implementation constraints:

* Internally, filtered truncate:

  * Copies retained data to a temporary table
  * Truncates the table
  * Copies data back

* This introduces significant data movement overhead and makes
performance dependent on:

  * The **ratio of deleted vs retained data**
  * **Index availability** on filter columns

* Experiments show:

* Faster than `DeleteAll()` only when deleting a **large portion of
data**
  * Can be **slower** when deletion ratio is low
  * Behavior becomes harder to predict with more complex filters


Given these trade-offs, this PR focuses on **full table truncation
only** to ensure predictable and safe performance.

---
#### Truncate Table Feature

* Added `"Truncate Allowed"` field to `Retention Policy Allowed Table`
* Introduced `SetTruncateAllowed` and `IsTruncateAllowed` logic in
relevant codeunits
* Added `TruncateAll` action on the **Retention Policy Setup Card**

#### Access Control & Safety
* Accessible to users with retention policy management permissions
* Requires **double confirmation dialogs** before execution
* Only applicable to explicitly allowed tables
* Includes telemetry, logging, and user feedback

---
Related items:
[PR243885](https://dynamicssmb2.visualstudio.com/Dynamics%20SMB/_git/NAV/pullrequest/243885)
Fixes #
[AB#546970](https://dynamicssmb2.visualstudio.com/Dynamics%20SMB/_workitems/edit/546970/)

---------

Co-authored-by: Jiawen Sun <t-jiawensun@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…7755)

<!-- Thank you for submitting a Pull Request. If you're new to
contributing to BCApps please read our pull request guideline below
* https://github.com/microsoft/BCApps/Contributing.md
-->
#### Summary <!-- Provide a general summary of your changes -->
When a user has only indirect delete permissions on a table (e.g.,
"Retention Policy Log Entry"), the `RecRef.Truncate(true)` call in
TruncateTableRecords fails because it requires direct permissions. This
PR adds an integration event pattern to delegate truncation to a
subscriber that holds the required indirect permissions.

#### Details
- Added OnTruncateRecordsIndirectPermissionRequired integration event to
Apply Retention Policy codeunit
- Added new codeunit 3921 Retention Policy Logs Truncate that subscribes
to the event and truncates the "Retention Policy Log Entry" table with
indirect permissions
- Modified TruncateTableRecords in RetentionPolicySetupImpl to check
effective delete permissions and raise the event when permissions are
indirect instead of truncating directly
- Updated object ID range in [app.json] from 3920 to 3921

#### How to Review
### System Application / Retention Policy
**Files:** `ApplyRetentionPolicy.Codeunit.al`,
RetentionPolicyLogsTruncate.Codeunit.al,
`RetentionPolicySetupImpl.Codeunit.al`, app.json
**Guidance:** Verify the integration event pattern is correct, the
indirect permission check logic is sound, and the new codeunit properly
handles the `Handled` pattern.


#### Work Item(s) <!-- Add the issue number here after the #. The issue
needs to be open and approved. Submitting PRs with no linked issues or
unapproved issues is highly discouraged. -->
Fixes
#[AB#631806](https://dynamicssmb2.visualstudio.com/Dynamics%20SMB/_workitems/edit/631806/)

---------

Co-authored-by: Jiawen Sun <t-jiawensun@microsoft.com>
@Jiawen-CS Jiawen-CS requested review from a team as code owners May 1, 2026 12:21
@Jiawen-CS Jiawen-CS added the 28.x Apply for release branch 28.x label May 1, 2026
@github-actions github-actions Bot added this to the Version 28.1 milestone May 1, 2026
@Jiawen-CS Jiawen-CS merged commit 1139bdf into releases/28.x May 4, 2026
43 checks passed
@Jiawen-CS Jiawen-CS deleted the backport/truncate-all-28x branch May 4, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

28.x Apply for release branch 28.x AL: System Application

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants