Unable to delete SQLite database file due to sharing violation IOException

I am using Unity 2018.3.11f1 on Windows 10

I am writing a program that copies a database file from outside of my Unity project’s folders to another location which is also outside of my project’s folders and then reads some data from the copy using SQLite.

It’s a very small program at the moment and there is only one place where a database connection is opened, which is then closed or disposed before I try deleting the file.

I would like to delete the database copy in OnApplicationQuit() but I keep getting this exception: “IOException: Sharing violation on path…”

I have spent quite a bit of time looking for answers all over and found and tried several suggestions without success, including:

  • Calling dbConn.Dispose() instead of dbConn.Close() before trying to delete

  • Calling GC.Collect() and GC.WaitForPendingFinalizers() before trying to delete

I have this issue in both the editor and standalone. In the editor, I can’t even delete the file directly from it’s folder, even after I stop play mode. I can, however, delete it directly from its folder after the program exits in standalone.

Edit 1: I wanted to add that I have tried using both System.IO.File.Delete and FileUtil.DeleteFileOrDirectory.

Edit 2: I also just tried calling Dispose() on all SqliteCommands before trying to delete and still not able to.

So the question then is: what else can I do to “release” the file so that it can be deleted?

Thanks much!

It seems like I have figured it out.

In order to be able to delete a db file after opening a connection to it and reading some data from it, I had to call Dispose() on the SqliteConnection and on all SqliteCommands and I also had to call Close() on all SqliteDataReaders.