How do I clear all the nodes so that it looks like it just started to run?

I just want to use python to help me do some jobs on some patients’ data. Each patient data is independent, and to prevent interference between data while the code is running, I want to be able to clear all currently saved state nodes after processing a patient data.

I have solved this.

if slicer.mrmlScene:
    tmpdict = slicer.util.getNodes(useLists=True)
    for name,tmplist in tmpdict.items():
        for node in tmplist:
            slicer.mrmlScene.RemoveNode(node)

I think your code would delete all the singleton nodes like the view nodes, etc.

This should achieve the same thing without deleting those:

slicer.mrmlScene.Clear()