LocalDB: How do you delete it?

C#Visual StudioSql Server-Express

C# Problem Overview


Setup: Entity framework code first to new database.

Scenario: I'm playing around with EF and I add a bunch of elements to my database. I then change the entity model, and while I know that I could do migrations, I just want to start from scratch and basically wipe the database from the earth.

The database used by default was (localdb)\v11.0.

My question is:

Can I go somewhere and just delete a file, or start some kind of manager to delete that database and start from scratch?

C# Solutions


Solution 1 - C#

Just go in command prompt with admin rights and type:

//list the instancies
sqllocaldb i

//stop selected instance
sqllocaldb p "selected instance"

//delete
sqllocaldb d "selected instance"

//recreate or create new one 
sqllocaldb c "new instance"

Solution 2 - C#

From Visual Studio => Click View => SQL Server Object Explorer=> Right click the desired database and choose delete and it will be deleted or do whatever you want

Solution 3 - C#

If you're using Entity Framework Core, you can enter this in the Package Manager Console:

PM> Drop-Database

It will drop the current database. This command will tell you which one:

PM> Get-DbContext

This is also handy:

PM> Get-Help about_EntityFrameworkCore

Instead of the Package Manager Console, you can also use the dotnet CLI via PowerShell or the command prompt:

PS> dotnet ef database drop

Make sure you install the EF extension first by running this command:

dotnet tool install --global dotnet-ef

Solution 4 - C#

I think you want to delete an individual database, not a LocalDB instance. If so, just issue a drop database command:

DROP DATABASE databasename;

You can do this from sqlcmd, Management Studio, your application code, maybe even Visual Studio...

Solution 5 - C#

Yes you can. In VS 2015/2017 press Ctrl+Q, type "object explorer". The "SQL Server Object Explorer" should open, where you'll see your local DB instances. Expand the DB instance, and you'll see the different databases. Select one database perform a right click and choose "Delete".

For additional information check this link.

Hope that helps.

Solution 6 - C#

There's an exe called SqlLocalDB.exe which can be found in C:\Program Files\Microsoft SQL Server\{version}\Tools\Binn

For a full delete:

>sqllocaldb stop InstanceName
>sqllocaldb delete InstanceName

Once that is done, you can optionally also remove the .mdf files from the disk. Mine were at

C:\Users\{username}

You can also recreate a fresh instance, and even attach the .mdf files you still want afterwards.

>sqllocaldb create InstanceName
>sqllocaldb start InstanceName
>sqllocaldb info InstanceName

You can see your instances at:

C:\Users\{username}\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances

Solution 7 - C#

With Entity Framework Core, this can also be accomplished form the the command line:

dotnet ef database drop --project [path to project]

Or if you have multiple database contexts:

dotnet ef database drop --project [path to project] --context [ContextName]

Make sure you install the EF extension first by running this command:

dotnet tool install --global dotnet-ef

Solution 8 - C#

LocalDB is its own separate server (its name suggests that it is just a database in some other server instance but this is not the case). In SQL Server 2014 Express you connect to it using server name "(localdb)\MSSQLLocalDB", just as you would connect to any ordinary database server. If you connect using SQL Server Management Studio then you have all the power of SSMS available to you.

Solution 9 - C#

Entity Framework 3.1:

> Drop Database

dotnet ef database drop

> Do not want to drop database, willing to delete tables in the database

dotnet ef database update 0

Solution 10 - C#

I wrote a batch file to delete an SQL local db. It uses trusted connection. If you want, you can add your credentials with -U Username -P Password parameters.

Check out sqlcmd documentation!

enter image description here

@echo off
set /p DbName=Enter local db name:
echo.
sqlcmd -e -S "(LocalDb)\MSSQLLocalDB" -d "master" -Q "EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'%DbName%'; USE [master]; DROP DATABASE [%DbName%];"
echo Finished!
pause > nul

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGabriel G. RoyView Question on Stackoverflow
Solution 1 - C#Filip GjorgjevikjView Answer on Stackoverflow
Solution 2 - C#Mohamed Salah DarwishView Answer on Stackoverflow
Solution 3 - C#Matt GregoryView Answer on Stackoverflow
Solution 4 - C#Aaron BertrandView Answer on Stackoverflow
Solution 5 - C#MoerwaldView Answer on Stackoverflow
Solution 6 - C#CRiceView Answer on Stackoverflow
Solution 7 - C#Mike GodinView Answer on Stackoverflow
Solution 8 - C#Dave ZifferView Answer on Stackoverflow
Solution 9 - C#Nick BView Answer on Stackoverflow
Solution 10 - C#Alper EbicogluView Answer on Stackoverflow