SQL Server error 929 ("Unable to close the database that is") appears when you try to take a database offline, detach it, restore over it, or drop it, and SQL Server cannot release it because something is still using it. The fix is to find and remove the active connection or dependency, then retry the operation. In practice this means checking who is connected, switching the database to single-user mode correctly, and only then running the close, detach, or drop. Nothing here requires third-party tools; everything is done in SQL Server Management Studio (SSMS) or with T-SQL, and you should have a current backup before you start.

What You'll Need

  • SQL Server Management Studio (SSMS) or another client that can run T-SQL queries.
  • Membership in the sysadmin fixed server role, or at minimum ALTER DATABASE permission on the target database plus permission to kill sessions.
  • A recent backup of the database, especially if you plan to detach or drop it.
  • The exact database name, and knowledge of whether any application, job, or linked server is set to use it.

Step-by-Step

  1. Confirm the error and the target database. Note the exact database name from the error message. Names are case-insensitive by default but spelling matters, and a database name with spaces must be wrapped in brackets, for example [My Database].
  2. Identify active connections. In a new query window connected to master, run:
    SELECT session_id, login_name, host_name, program_name, status FROM sys.dm_exec_sessions WHERE database_id = DB_ID('YourDatabase');
    This lists every session currently attached to that database. Look at program_name and host_name to see whether it is an application, a SQL Server Agent job, or your own SSMS window.
  3. Close your own connection first. A very common cause is that you are sitting in a query window pointed at the database you are trying to take offline. Change the database dropdown in SSMS to master, or open a fresh connection to master, and retry. Many 929 errors disappear at this point.
  4. Check for blocking dependencies. If a job, replication, database mirroring, or a linked server is using the database, stop it. For SQL Server Agent jobs, disable the job or stop the job before retrying. For replication, remove the subscription or publication as appropriate.
  5. Put the database into single-user mode with rollback immediate. From a connection to master, run:
    ALTER DATABASE [YourDatabase] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
    The ROLLBACK IMMEDIATE clause terminates open transactions and disconnects other sessions. This is the step that actually clears the remaining connections. Be aware it rolls back any uncommitted work, so do it during a maintenance window.
  6. Immediately perform the operation you wanted. While the database is in single-user mode, run your intended command, for example:
    ALTER DATABASE [YourDatabase] SET OFFLINE WITH ROLLBACK IMMEDIATE;
    or DROP DATABASE [YourDatabase];
    or the detach via SSMS (right-click the database, Tasks, Detach). Do this in the same session; single-user mode allows only one connection, and if another process grabs it first you will see the error again.
  7. Return to multi-user mode if you kept the database. If you took it offline and later bring it back online, run:
    ALTER DATABASE [YourDatabase] SET MULTI_USER;
    Skipping this leaves the database usable by only one connection and will cause confusing failures for applications.
  8. Verify the result. Run SELECT name, state_desc FROM sys.databases WHERE name = 'YourDatabase'; The state should read OFFLINE, or the row should be gone if you dropped it. For a detach, confirm the files are no longer listed in sys.master_files.

Common Mistakes

  • Running the ALTER DATABASE command while connected to the target database. The command needs a connection to master; otherwise you are one of the sessions holding the database open.
  • Using SINGLE_USER without ROLLBACK IMMEDIATE. Without the rollback clause, the statement waits for existing transactions to finish and can hang or time out.
  • Forgetting to switch back to MULTI_USER. Applications then fail to connect, and the cause is not obvious.
  • Killing sessions one by one and assuming it is done. New connections can appear between kills. Single-user mode closes the door.
  • Detaching or dropping without a backup. Detach leaves the files on disk, but drop deletes the database and its files. Back up first.
  • Ignoring SQL Server Agent jobs. A scheduled job reconnecting every few minutes will keep producing the error even after you clear sessions.

Tips

  • Do this during a maintenance window. ROLLBACK IMMEDIATE discards in-flight transactions, which can look like data loss to users.
  • Record the session details (login, host, program) before killing anything, so you can tell the application owner what was connected.
  • If the database is part of an Availability Group, take it out of the group first; you cannot simply set an AG database to single-user mode.
  • Use sys.dm_exec_sessions and sys.dm_tran_locks together if you need to see what a session is waiting on.
  • Keep a scripted version of the single-user, offline, multi-user sequence handy for repeat maintenance.

Troubleshooting

Error persists after SINGLE_USER. Another session may have taken the single connection. Re-run the ALTER DATABASE statement in a fresh master connection and perform the operation in the same batch if possible.

ALTER DATABASE fails with a permissions error. You need ALTER on the database or sysadmin membership. Ask an administrator to grant it.

The database is in a restoring or recovery state. Wait for recovery to finish, or check the SQL Server error log for the underlying cause before retrying.

You cannot drop because of replication. Remove the publication or subscription first with the replication stored procedures, then retry the drop.

You cannot detach because of a snapshot or mirror. Drop the database snapshot, or remove mirroring, then retry.

Nothing works and the database is corrupted. Stop and restore from backup rather than forcing the operation. Forcing a detach or drop on a damaged database can make recovery harder.

Frequently Asked Questions

What does error 929 actually mean?

SQL Server could not close the database because at least one session or dependency still holds it open. It is a locking/connection problem, not corruption.

Is ROLLBACK IMMEDIATE safe?

It is safe for the server but not for in-flight work: uncommitted transactions are rolled back. Use it in a maintenance window and take a backup first.

Why does the error come back after I kill sessions?

Something reconnects, usually an application pool, a SQL Server Agent job, or a linked server. Find and stop that source, or use single-user mode to block new connections.

Can I take an Availability Group database offline this way?

No. Remove the database from the Availability Group first, then perform the operation on the primary replica.

Do I need to restart SQL Server?

Usually not. A restart clears connections but is disruptive and does not fix the underlying cause, such as a job that reconnects.

Does detaching delete my data?

No, detach leaves the .mdf and .ldf files on disk. Dropping the database does delete them, so back up before dropping.