function cleanSlicerArtifacts(model)
%

%   Copyright 2019-2021 The MathWorks, Inc.

    slicerConfig = SlicerConfiguration.getConfiguration(model);
    msObj = slicerConfig.modelSlicer;

    % In some cases, terminate(in close) does not go through cleanly when
    % slicer dialog is closed. This check ensures it is terminated cleanly
    % Related geck: g2006118
    if msObj.compiled
        msObj.terminateModel;
    end

    if get_param(msObj.modelH,'ModelSlicerActive') ~= ModelSlicer.Terminated
        % Exiting from editable highlight
        msObj.setModelSlicerActive(ModelSlicer.Terminated)
    end

    % If this is an embedded DDG we need to destroy the embedded DDG
    % component and return the studio to its original position
    if msObj.useEmbeddedDDG
        ed = SlicerConfiguration.findEditor(get_param(msObj.modelH,'Name'));
        studio = ed(1).getStudio;
        studio.setStudioPosition(msObj.origStudioPos);
    end

    % If only useCvd is true but no covdata available,
    % we turn it off so user shouldn't confused when the dialog is opened next time.
    for i=1:length(slicerConfig.sliceCriteria)
        sc = slicerConfig.sliceCriteria(i);
        sc.checkDeletedItems();
        if ~isempty(sc.overlay)
            sc.overlay.removeFromEditor();
            sc.overlay.clearAll();
        end
        if sc.useCvd
            if ~isempty(sc.sdiViewObj)
                if msObj.isThisUsingSdi()
                    if i == slicerConfig.selectedIdx
                        sc.sdiViewObj.saveSessionToTempFile();
                    end
                end
                sc.sdiViewObj.updateSessionInSlicexFile();
            end
            sc.removePortsLabelled();
            if isempty(sc.cvd)
                sc.useCvd = false;
                sc.cvFileName = '';
            end
        end
        sc = [];
    end

    msObj.clearHighlighter();
    msObj.releaseSdi();
    slicerConfig.saveConfigurationToFile();
    SlicerConfiguration.configurationMapper('set',slicerConfig.modelH,[]);

    % Delete dialogs
    if isa(msObj.dlg,'DAStudio.Dialog')
        dlgSrc = msObj.dlg.getSource;
        if isa(dlgSrc.optionDlg,'DAStudio.Dialog')
            delete(dlgSrc.optionDlg)
        end
        if isa(dlgSrc.runSimDlg,'DAStudio.Dialog')
            dlgSrc.runSimDlg.getSource.mdlClose = true;
            delete(dlgSrc.runSimDlg);
        end
        if isa(dlgSrc.runSldvDlg,'DAStudio.Dialog')
            dlgSrc.runSldvdlg.getSource.mdlClose = true;
            delete(dlgSrc.runSldvDlg);
        end
    end

    modelslicerprivate('slicerMapper', 'set',msObj.modelH,[])
    ModelSlicer.removeModelCloseCallback(msObj.modelH);

    % Remove the slicer from referenced models
    if  ~isempty(msObj.refMdlToMdlBlk)
        refModelHs = msObj.getReferencedModels();
        for idx = 1:numel(refModelHs)
            mdl = refModelHs{idx};
            modelslicerprivate('slicerMapper', 'set', mdl, []);
        end
    end

    % Notify event for restoring SimIn and model parameters
    % applied by slicer debugger
    notify(msObj,'eventModelSlicerDialogClosed');
end
