Typically when we need to delete a list of filtered records in Business Central, we would either loop through the recordset to delete each of the records or use the "DeleteAll" function. However, it is not recommended to use the "DeleteAll" function.
Why is that so?
The "DeleteAll" function will cause the system to perform a lock even when there is nothing to delete. Hence, affecting the system performance to some extent.
Is there a solution?
Yes. There is a better way to implement it to minimize the number of locks. We can use the "IsEmpty" function to check if any record exists before calling the "DeleteAll" function. In this case, we can avoid triggering locks unnecessarily.
procedure ReduceDeleteAllLocks(Item: Record Item)
begin
Item.SetRange("Location Code", 'OLDWAREHOUSE');
if not Item.IsEmpty() then
Item.DeleteAll();
end;
References:
(1) https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-isempty-method
(2) https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-deleteall-method