How to clean up temp file after cropping volume

Hi,
I have a script that automatically crops (and resamples) my volumes in Slicer. It works fine, but every time it runs, it leaves a copy of the original volume in the temp folder. In my case, here:
c:\Users\tamas\AppData\Local\Temp\Slicer\

I just realized that this folder was over 100 GB today. I’m not sure if this would be cleaned up automatically after a while. But I would prefer a way to clean it up from my python script immediately after resampling my volumes.
Does anybody know how to clean up the temp folder of Slicer?
Or prevent the crop volume logic to create these large temp files? (I have lots of RAM.)

Here is a minimal script to reproduce the issue. You will need the MRHead image and a ROI in the scene to run this:

inputVolume = slicer.util.getNode('MRHead')
roi = slicer.util.getNode('R')

cropVolumeNode = slicer.vtkMRMLCropVolumeParametersNode()
cropVolumeNode.SetScene(slicer.mrmlScene)
slicer.mrmlScene.AddNode(cropVolumeNode)

cropVolumeNode.SetInputVolumeNodeID(inputVolume.GetID())
cropVolumeNode.SetROINodeID(roi.GetID())
# cropVolumeNode.SetOutputVolumeNodeID(outputVolume.GetID()) # This is optional

cropVolumeLogic = slicer.modules.cropvolume.logic()
cropVolumeLogic.Apply(cropVolumeNode)

Thanks,
Tamas

CLI module inputs/outputs are preserved if developer mode is enabled in application settings. You can override this setting by calling DeleteTemporaryFilesOn() for a specific CLI module’s logic.

Probably we should make this a separate checkbox within the developer section in application settings.

Thank you!
It took me a while to figure out, but I realize now that the logic of the crop volume module has a CLI module in it:
slicer.modules.cropvolume.logic().GetResampleLogic().DeleteTemporaryFilesOn()

1 Like

I’ve added a separate option to control deletion of CLI temporary files in application settings / Developers section: “Preserve CLI module data files”. It’ll be available in tomorrow’s Preview Release.

1 Like

Thank you for adding this option!

1 Like