function clearFunction( varargin )
; %#ok Undocumented

% This function provides a clean workspace in which to call clear
% so that the call does not interfere with the base workspace, where
% clear classes and other such calls might end up affecting the 
% variables stored there.

% Copyright 2009-2020 The MathWorks, Inc.

dctSchedulerMessage( 5, '> distcomp.clearFunction' );

session = parallel.internal.pool.workerSession();
if isempty( session ) || isempty( session.Session )
    itemsToClear = [];
else
    itemsToClear = session.Session.drainItemsToClear( varargin{:} );
end
itemCount = numel( itemsToClear );
dctSchedulerMessage( 6, '- distcomp.clearFunction: items to clear : %d', itemCount );

for ii = 1:itemCount
    % Unpack each serializedPathInfo, and attempt a clear on each, one at a time.
    pathInfo = itemsToClear( ii );
    dctSchedulerMessage( 6, '- clearfunction pathInfo %s', pathInfo.getDebugString() );
    
    clearArgs = pathInfo.RawPaths;
    for clearArgsIdx = 1:numel( clearArgs )
        try
            arg = char( clearArgs{ clearArgsIdx } );
            [ pathTranslated, workerPath ] = pathInfo.MATLABDrivePathMap.translatePath( arg );
            dctSchedulerMessage( 6, '%s clearing: %s. pathTranslated %d', mfilename, workerPath, pathTranslated );
            clear( workerPath );
        catch E
            dctSchedulerMessage( 0, 'Encountered error:', E);
        end
    end
end

dctSchedulerMessage( 5, '< distcomp.clearFunction' );
end
