question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Delete instance files in %USERPROFILE%

See original GitHub issue

Describe the bug

When running the following snippet, not all instance files are deleted. Within the %USERPROFILE%, the following two files are still there:

  • <<MyDatabase>>.mdf
  • <<MyDatabase>>_log.ldf

This leads to further errors when trying to recreate the database.

Steps To reproduce

using var localDb = new SqlLocalDbApi();
localDb.DeleteInstance("MyInstance", true);

Expected behaviour

There should be no more files belonging to the instance MyInstance under %USERPROFILE%.

Actual behaviour

There are still two files belonging to the instance MyInstance under %USERPROFILE%:

  • <<MyDatabase>>.mdf
  • <<MyDatabase>>_log.ldf

System information

  • OS: Windows 10, Version 20H2
  • Library Version: 3.0.1
  • SQL LocalDB version: 13.0
  • .NET version:
.NET SDK (reflecting any global.json):
 Version:   6.0.100-rc.1.21463.6
 Commit:    e627d556a1

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100-rc.1.21463.6\

Host (useful for support):
  Version: 6.0.0-rc.1.21451.13
  Commit:  d7619cd4b1

.NET SDKs installed:
  2.1.700 [C:\Program Files\dotnet\sdk]
  2.2.300 [C:\Program Files\dotnet\sdk]
  3.1.412 [C:\Program Files\dotnet\sdk]
  5.0.206 [C:\Program Files\dotnet\sdk]
  5.0.303 [C:\Program Files\dotnet\sdk]
  5.0.401 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.1.21463.6 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-rc.1.21452.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.1.21451.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.1.21451.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Additional context

I think all existing databases within the instance should be deleted here.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
martincostellocommented, Oct 6, 2021

Right, having looked into this, this isn’t a bug.

The databases created by SQL LocalDB are created at a location controlled by SQL Server itself, and as specified by its defaults and the connection string the user provides. They are not files intrinsically associated with the instance, because they are portable and can be attached to any other instance at any time.

There is no way for the library to know what you choose to do with the instance once you create it, and there is no way for it to know what the name of any database files you chose to create with it to delete would be. Guessing at names would be dangerous as it could lead to data loss if the guess was wrong. It is also not possible for it to determine what directory the database is in. %USERPROFILE% is just the SQL Server LocalDB default, it could be placed somewhere else.

The onus is on you as the creator of the database(s) to keep track of them and tidy them up as your requirements may dictate.

For example, there is this code in the test console app:

https://github.com/martincostello/sqllocaldb/blob/fd6c2262ec4f75a0564c1558d6f6c30534e6f2db/src/TestApp/Program.cs#L101-L102

After the first line is executed, %USERPROFILE%\MyDatabase.mdf and %USERPROFILE%\MyDatabase_log.ldf will both exist. After the second line is executed, both files will be deleted by the SQL Server instance.

1reaction
mu88commented, Oct 6, 2021

Don’t worry, no problem 😀

You can reproduce it the following way:

  1. Execute the following code:
using var localDb = new SqlLocalDbApi();
var instance = localDb.GetOrCreateInstance("MyInstance");
var instanceManager = instance.Manage();
if (!instance.IsRunning)
{
    instanceManager.Start();
}
  1. Connect to the instance with SQL Server Management Studio using (localdb)\MyInstance and create a new database MyDatabase.
  2. Execute the following code:
using var localDb = new SqlLocalDbApi();
localDb.StopInstance("MyInstance");
localDb.DeleteInstance("MyInstance", true);

When running this on my machine, the following two files remain there:

  • %USERPROFILE%\MyDatabase.mdf
  • %USERPROFILE%\MyDatabase_log.ldf
Read more comments on GitHub >

github_iconTop Results From Across the Web

Scripting : how to delete files in user profile
You can write VBScript to delete files from <UserProfile>.. Find out what are the folders available in C:\Users folder...(If you are working on...
Read more >
OD i Install - Deleting an instance
Delete the instance directory which is under /QIBM/UserData/OnDemand. If you use the WRKLNK command each directory must be empty before it can be...
Read more >
Files from User profile deleted totally
When i opend C:/Users/Myprofile it is totally empty. On every reboot windows created some kind of instance on my user profile like in...
Read more >
Use PowerShell delete a user profile (step-by-step guide)
Learn the step by step guide on how to use Powershell to delete user profile of Windows users using the CIM command.
Read more >
Powershell script completely removing user profile/folder ...
So when I run the script, it seems the Remove-CimInstance isn't working because the profiles are still present at the login screen, in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found