Cleaning up local Dicom Database files

I noticed that the local dicom database files stick around even when you delete all the patients, and will continue growing in size as more patients are added and removed. I would like to clean up these files to prevent any issues if a computer has to process many patients.

how would i do this in slicer? I know how to delete the files themselves, but they are in use during the runtime of the application as far as i can tell, and will throw an error of ‘File in use’

code i’m using:
import os, shutil
dir = os.path.dirname(slicer.dicomDatabase.databaseFilename)
shutil.rmtree(dir)

Probably the easiest would be to open a new database directory and then it should be possible to delete the old one.

Although this block seems to indicate it’s not possible:

@cpinter may also have ideas why this fails - perhaps we need to find out where the database files (or directory?) are still in use.

There could be multiple connection to the database from multiple modules. You probably need to restart Slicer to make sure none of them still uses the old database file.

so you are saying that the database couldn’t be cleared out in one session? is there any way to close down all the modules that might be using it, maybe when the application is closing, and just run the code for removing the files just before the app quits? or is there a place in slicer where data can be stored to persist until the next session where you could store the last database location and remove it when the next session runs?

The easiest is to switch to a new (empty) DICOM database folder. The old folder can be deleted once you closed Slicer.

that method should work for me. I just need to find how to set the local database folder through python, I’m not currently seeing the method to do that, could you point me in the right direction?

thanks.

slicer.dicomDatabase.databaseFilename

that is a way to access the current database, but it is not a writable variable.

Also, it points to the sql data file in the folder. If i just assigned that to a new path with the same data file name, would it create a new file with the given name or would i have to initialize the database somehow?

ok, it looks like I can use the dicom utils to open the database, which will set the current database to the passed path.

example:
import DICOMLib.DICOMUtils as utils
utils.openDatabase(newDir)

2 Likes