function storedEnv = pClearEnvironmentBeforeSubmission
; %#ok Undocumented
%pClearEnvironmentForExecution - clear various environment variables
%   Various schedulers (such as LSF) default to forwarding all environment
%   variables to the workers. This function clears environment variables
%   that could interfere with correct operation on the worker.

% Copyright 2006-2023 The MathWorks, Inc.

% Might want to add HOME to the list below?
fields = {'ARCH', 'MATLAB', 'MATLABPATH', 'MATLAB_ARCH', 'BASEMATLABPATH', 'OS'};

% Ensure remote workers do not inherit Package-ness of client.
% TODO g2702048 remove this when Packages are enabled everywhere.
fields{end+1} = 'MW_FEATURE_packages';

storedEnv = struct;
for ii = 1:length(fields)
    oldVal = getenv(fields{ii});
    if ~isempty(oldVal)
        storedEnv.(fields{ii}) = oldVal;
        setenv(fields{ii}, '');
    end
end
