views:

768

answers:

5

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?

+1  A: 

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?

Chris Ballance
Nothing like IIS is accessing this DB, and when I look at the process list, I am not seeing anything at all using it. You may be right about restarting the server. It's a production system with other databases, so I hate to do that, but I might just have to.
jeremcc
I figured this was a production box, so I didn't want to recommend the restart, but that is a sure way to close all active connections. If you could post all the processes running on the box, one might jump out as the culprit
Chris Ballance
A: 

No One else should be using the DB, including yourself.

Nope, nobody is. I'm just trying to delete it.
jeremcc
+1  A: 

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.

MrTelly
Yeah, I did all that. There are no processes accessing it, and it still can't get a lock on it.
jeremcc
Do you need to set the DB in single user mode? Even so if you can't see it in Activity Monitor I'm inclined to agree that a Sql Server restart will solve the problem.
MrTelly
+2  A: 

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.

le dorfier
There's nothing in the list currently. But you know, there *were* a lot of transactional locks on this database, and I manually killed them all, not too long ago. It might still be in some sort of rollback state from that. Maybe I just need to wait. Hmm...
jeremcc
Very, very likely.
le dorfier
+1  A: 

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.

Remus Rusanu
Nice and specific, thanks.
jeremcc