I am trying to delete an existing database in SQL Server 2005. My first attempt produced the following error:
5030: The database could not be exclusively
locked to perform the operation.
I have since killed all processes that are accessing the database. I have also removed the replication subscription that it had previously been involved in.
Any thoughts on what else that could be holding the lock on it besides SQL Server processes and replication?
Update: I restarted the server, and that fixed it. I was trying to avoid that, since this is a production server, but hey what can you do?
asked Feb 8, 2009 at 5:14
A production server in which so many connections use the database yet you want to drop it? 🙂
None the less, how to kick out everybody from the database:
USE [dbname];
ALTER DATABASE [dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
Then drop the database:
USE [master];
DROP DATABASE [dbname];
There is still a very small window of opportunity between the USE [master]; and DROP DATABASE ... where some other connection can grab the 1 single allowed lock on the database, but it usually not worth working around that.
answered Nov 23, 2009 at 20:05
Remus RusanuRemus Rusanu
285k40 gold badges429 silver badges564 bronze badges
3
I hate to say it, but a quick solution is to restart the system, make sure the sql server server service is not started, then you should be able to delete.
Also, is IIS stopped if you db is connected to a web ap?
answered Feb 8, 2009 at 5:19
Chris BallanceChris Ballance
33.5k26 gold badges103 silver badges150 bronze badges
2
You don’t happen to know if anyone left a transaction in an uncompleted rollback state (or otherwise uncompleted)? Might as well check the locks list, too.
answered Feb 8, 2009 at 5:34
dkretzdkretz
37.2k13 gold badges80 silver badges137 bronze badges
1
In the management studio, goto Management->Activity Monitor (right click) -> View Processes. That will give you a full list of everything running, you can sort the list by Database to see what is still attached, and you can also kill any connections. It’s easy to end up with orphaned connections that will prevent you from getting the exclusive access that you need.
answered Feb 8, 2009 at 5:19
MrTellyMrTelly
14.6k1 gold badge47 silver badges80 bronze badges
2
No One else should be using the DB, including yourself.
answered Feb 8, 2009 at 5:19
1
Why would we make a deleted DB to multi user mode.
ALTER DATABASE dbName SET MULTI_USER WITH ROLLBACK IMMEDIATE
Soner Gönül
95.9k102 gold badges205 silver badges356 bronze badges
answered Jun 13, 2014 at 7:55
To avoid this error, use the T-SQL script below in the master database. Make sure to run this (and modify the @dbname) for each database you are running the ALTER DATABASE command in.
«The database could not be exclusively locked to perform the
operation»
This «connection killer» script will work if Windows has established JDBC connections to the database. But this script is unable to kill off JDBC connections for Linux services (e.g. JBoss). So you’ll still get that error if you don’t stop JBoss manually. I haven’t tried other protocols, but please comment if you find out more information as you build new systems.
USE master;
DECLARE @dbname sysname
Set @dbname = 'DATABASE_NAME_HERE-PROD'
Declare @spid int
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname)
While @spid Is Not Null
Begin
Execute ('Kill ' + @spid)
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname) and spid > @spid
End
answered Oct 28, 2015 at 20:26
![]()
MacGyverMacGyver
18k38 gold badges157 silver badges242 bronze badges
This error normally occurs when your database is in Multi User mode where users are accessing your database or some objects are referring to your database.
First you should set the database to single user mode:
ALTER DATABASE dbName
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Now we will try to delete the database
delete DATABASE ...
Finally set the database to Multiuser mode
ALTER DATABASE dbName
SET MULTI_USER WITH ROLLBACK IMMEDIATE
answered Sep 16, 2013 at 5:37
- Remove From My Forums
-
Question
-
I am trying to rename the database but i am getting below exception while doing it—>
Error: the database could not be exclusively locked to perform the operation.(Microsoft Sql Server,Error 5030)
Thanks.
Answers
-
Thats because someone else is accessing the database.. Put the database into single user mode the rename it.
USE [master]; GO ALTER DATABASE foo SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO EXEC sp_renamedb N'foo', N'bar';
vt
Please mark answered if I’ve answered your question and vote for it as helpful to help other user’s find a solution quicker
-
Proposed as answer by
Tuesday, October 23, 2012 9:15 AM
-
Marked as answer by
Maggy111
Tuesday, October 23, 2012 9:22 AM
-
Proposed as answer by
- Remove From My Forums
-
Question
-
I am trying to rename the database but i am getting below exception while doing it—>
Error: the database could not be exclusively locked to perform the operation.(Microsoft Sql Server,Error 5030)
Thanks.
Answers
-
Thats because someone else is accessing the database.. Put the database into single user mode the rename it.
USE [master]; GO ALTER DATABASE foo SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO EXEC sp_renamedb N'foo', N'bar';
vt
Please mark answered if I’ve answered your question and vote for it as helpful to help other user’s find a solution quicker
-
Proposed as answer by
Tuesday, October 23, 2012 9:15 AM
-
Marked as answer by
Maggy111
Tuesday, October 23, 2012 9:22 AM
-
Proposed as answer by
- Remove From My Forums
-
Question
-
I am trying to rename the database but i am getting below exception while doing it—>
Error: the database could not be exclusively locked to perform the operation.(Microsoft Sql Server,Error 5030)
Thanks.
Answers
-
Thats because someone else is accessing the database.. Put the database into single user mode the rename it.
USE [master]; GO ALTER DATABASE foo SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO EXEC sp_renamedb N'foo', N'bar';
vt
Please mark answered if I’ve answered your question and vote for it as helpful to help other user’s find a solution quicker
-
Proposed as answer by
Tuesday, October 23, 2012 9:15 AM
-
Marked as answer by
Maggy111
Tuesday, October 23, 2012 9:22 AM
-
Proposed as answer by