# How to clean up temp file after cropping volume

**URL:** <https://discourse.slicer.org/t/how-to-clean-up-temp-file-after-cropping-volume/12939>\
**Category:** Support\
**Tags:** cropping\
**Created:** [August 11, 2020, 1:55am UTC](https://discourse.slicer.org/t/how-to-clean-up-temp-file-after-cropping-volume/12939 "2020-08-11T01:55:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)\
**Post date:** [August 11, 2020, 1:55am UTC](https://discourse.slicer.org/t/how-to-clean-up-temp-file-after-cropping-volume/12939/1 "2020-08-11T01:55:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 11, 2020, 2:30am UTC](https://discourse.slicer.org/t/how-to-clean-up-temp-file-after-cropping-volume/12939/2 "2020-08-11T02:30:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)\
**Post date:** [August 11, 2020, 4:14am UTC](https://discourse.slicer.org/t/how-to-clean-up-temp-file-after-cropping-volume/12939/3 "2020-08-11T04:14:55Z")

</div>

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()`

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 11, 2020, 4:41am UTC](https://discourse.slicer.org/t/how-to-clean-up-temp-file-after-cropping-volume/12939/4 "2020-08-11T04:41:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)\
**Post date:** [August 11, 2020, 4:44am UTC](https://discourse.slicer.org/t/how-to-clean-up-temp-file-after-cropping-volume/12939/5 "2020-08-11T04:44:12Z")

</div>

Thank you for adding this option!
