The answer is that onCleanup() works perfectly on these conditions.
- A public variable onCleanupArray on the app stores all the onCleanUp(s).
- Since, the db instance is persistent type and other variables from the app cannot be accessed in the cleanUp, except if it is a local variable (dbNo) and is passed to the cleanup (cleanUpTasks) function can be accessed.
- Here, the cleanUpTasks gets the persistent db value and executes to remove the insert.
app.onCleanupArray{1} = onCleanup(@()cleanUpTasks(dbNo));
For other example how it used, such as in Window Management:
function openWindow(winM, newWindow, varargin)
% varargin{1}: must be of type "matlab.apps.AppBase"
slot = nextSlot(winM);
winM.subwindows{slot} = newWindow;
% Technique to delete subwindow on parent deletion
% This method came about with problems of MATLAB not executing
% closeRequest callback in emergency/force quit
if ~isempty(varargin)
parentAppObj = varargin{1};
% onCleanupArray: must be public variable added to windows
% making this call
parentAppObj.onCleanupArray{end+1} = onCleanup(@()delete(winM.subwindows{slot}));
end
end