LocalDB: How do you delete it?
Asked Answered
L

11

65

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?

Light answered 10/3, 2013 at 4:15 Comment(1)
+1 This is a good an interesting question to start working with EF.Karlmarxstadt
L
75

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"
Larochelle answered 12/1, 2014 at 10:45 Comment(2)
Deleting the instance does not delete the actual database file. When EF recreates it the existing MDF file is (sometimes) reused.Puga
Awesome, thanks. That should be the accepted answer. Regarding the previous comment: That's a program call 'file explorer' becomes handy ;-)Conwell
E
26

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

Extortion answered 3/2, 2014 at 23:26 Comment(1)
You delete the connection to the database, not the database itselfTick
H
20

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
Highly answered 7/11, 2017 at 2:10 Comment(1)
This is exactly what I was looking for.Anglosaxon
A
9

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...

Atomy answered 10/3, 2013 at 4:18 Comment(0)
A
6

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
Aerography answered 22/7, 2020 at 6:4 Comment(0)
G
4

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.

Gurtner answered 18/4, 2017 at 7:57 Comment(0)
P
2

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
Ptolemaeus answered 21/5, 2020 at 11:17 Comment(0)
L
1

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.

Loup answered 16/3, 2016 at 23:58 Comment(0)
P
0

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
Phoenicia answered 22/5, 2020 at 3:44 Comment(0)
C
0

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
Consuela answered 26/5, 2021 at 13:57 Comment(0)
S
0

If you are have either of these errors:

  • FormatMessageW failed. Error code returned: 15100
  • FormatMessageW failed. Error code returned: 15105

then check to make sure you are using the correct version of sqllocaldb.exe.

You can verify the version by using:

where.exe sqllocaldb

Which might output, for example:

C:\Program Files\Microsoft SQL Server\150\Tools\Binn\SqlLocalDB.exe
C:\Program Files\Microsoft SQL Server\130\Tools\Binn\SqlLocalDB.exe

Depending on the order in which you install SQL Server, the newer version could be later in the path and it will report the error above.

Stick answered 22/3, 2023 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.