Clear/ClearAll does not reset SingleInstance codeunit state
SingleInstance codeunits keep their global variables for the lifetime of the session. A developer who calls Clear() on a SingleInstance codeunit variable — or ClearAll() in a codeunit that holds SingleInstance codeunit references — expects the stored state to reset. It does not.
ClearAll does not affect or change values for variables in single instance codeunits.
— System.ClearAll() Method on Microsoft Learn
Clear() only deletes the local reference to the codeunit, not the codeunit’s internal state. The cached values — document numbers, flags, counters — survive both calls. Subsequent code that reads from the singleton still sees the old state with no indication that the reset had no effect. If the SingleInstance codeunit needs a reset path, expose an explicit procedure that calls ClearAll() from inside the codeunit itself.
Example
codeunit 50100 MyCodeunit
{
procedure ResetCache()
var
CacheMgmt: Codeunit "Cache Management";
begin
Clear(CacheMgmt); // Clear/ClearAll does not reset SingleInstance codeunit state [PC0016]
end;
}Call an explicit reset procedure on the SingleInstance codeunit instead:
codeunit 50100 MyCodeunit
{
procedure ResetCache()
var
CacheMgmt: Codeunit "Cache Management";
begin
CacheMgmt.Reset();
end;
}The same diagnostic is raised when ClearAll() is called in a codeunit that holds a SingleInstance codeunit variable, either as a local or a global:
codeunit 50100 MyCodeunit
{
var
CacheMgmt: Codeunit "Cache Management";
procedure DoSomething()
begin
ClearAll(); // Clear/ClearAll does not reset SingleInstance codeunit state [PC0016]
end;
}Exception
The diagnostic is not raised when the SingleInstance codeunit’s global variables are all of type Record. Clear() and ClearAll() do not affect Record variables either way, so the behavior gap that the rule targets does not apply.
The diagnostic is also not raised when a SingleInstance codeunit calls ClearAll() on itself. ClearAll() invoked from inside the SingleInstance codeunit does reset its own global state — this is in fact the recommended way to implement a reset procedure.
See also
- System.Clear(var Any) Method on Microsoft Learn
- SingleInstance Property on Microsoft Learn
- Are you Clear on how Clear() works in AL? by Erik Hougaard