SQL Server Error 1406: Safe Database Removal Fix
In this article
SQL Server error 1406 (“Unable to force service safely remove database”) typically appears when you try to drop or detach a database that is still in use, or when the SQL Server service cannot be stopped cleanly. The direct answer: you cannot force the removal while the database is active. You must first take the database offline or stop the dependent service, then drop it using normal T-SQL or SSMS. This guide walks through the safe, supported steps to clear the error without corrupting other databases or the instance.
What You'll Need
- Administrative access to the SQL Server instance (sysadmin or db_owner).
- SQL Server Management Studio (SSMS) or Azure Data Studio, or the sqlcmd command-line tool.
- Permission to stop and start the SQL Server service (Windows service control).
- A current backup of the database you intend to remove, in case you need to restore it.
- Knowledge of any applications or jobs that connect to the database, so you can stop them first.
Step-by-Step
- Identify what is holding the database open. In SSMS, run:
SELECT session_id, login_name, status, program_name FROM sys.dm_exec_sessions WHERE database_id = DB_ID('YourDatabaseName');
This lists active sessions. Note the program names (e.g., an app server, a reporting tool) so you can stop them. - Stop dependent applications and jobs. Shut down any application, web service, or SQL Agent job that connects to the database. If a job is running, stop it from SQL Server Agent or wait for it to finish. This is safer than killing sessions.
- Set the database to single-user mode (optional but helpful). If sessions persist, run:
ALTER DATABASE YourDatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
This rolls back open transactions and restricts access to one connection. Use with caution: it will terminate active connections. - Take the database offline. Run:
ALTER DATABASE YourDatabaseName SET OFFLINE WITH ROLLBACK IMMEDIATE;
This releases file locks and makes the database unavailable. If this succeeds, the error is cleared for the next step. - Drop the database. With the database offline, run:
DROP DATABASE YourDatabaseName;
If you only need to detach, usesp_detach_db 'YourDatabaseName';instead. Dropping deletes the database and its files; detaching leaves the files for reattachment. - If the error persists, stop the SQL Server service. Open Windows Services (services.msc), find “SQL Server (MSSQLSERVER)” or your named instance, right-click, and choose Stop. Wait for it to stop completely. Then start it again. This clears any stuck service state that prevents forced removal.
- Retry the drop or detach. After the service restarts, connect again and run the DROP or detach command. If the database was already offline, you may need to bring it online first:
ALTER DATABASE YourDatabaseName SET ONLINE;then drop. - Verify removal. Run
SELECT name FROM sys.databases;to confirm the database is gone. Check that the physical .mdf and .ldf files are deleted if you dropped it; if not, delete them manually only after confirming the database is no longer attached.
Common Mistakes
- Killing sessions instead of stopping the application. Killing a session can leave transactions in a rollback state, prolonging locks. Stop the source first.
- Using SINGLE_USER without ROLLBACK IMMEDIATE. Without the rollback clause, the command waits indefinitely for other connections to close.
- Forgetting to restart the service after a failed force. The error often stems from a service that did not stop cleanly; a restart resets that state.
- Dropping a database that is still in use by replication or mirroring. Check for replication, mirroring, or log shipping before dropping; remove those relationships first.
- Deleting files manually while the database is still attached. This corrupts the instance. Always detach or drop through SQL Server first.
Tips
- Always back up the database before dropping or detaching, even if you think it is unused.
- Use
sp_who2to see all connections and their status; look for “sleeping” sessions that still hold locks. - If the database is part of an availability group, remove it from the group before dropping.
- Schedule maintenance windows for these operations; forcing single-user mode can disrupt users.
- Keep a script of the drop/detach commands for repeatable cleanup in test environments.
Troubleshooting
If the error continues after following the steps, check the SQL Server error log for messages about the database being in use or the service failing to stop. Look for entries around the time of your attempt. If the service will not stop, check for dependent services (e.g., SQL Server Agent) and stop them first. If the database is suspect or in recovery, you may need to run DBCC CHECKDB or restore from backup before dropping. In rare cases, the database may be marked as read-only or offline at the file system level; ensure the SQL Server service account has full control over the database files and folder. If you cannot drop because of a pending mirroring or replication configuration, remove those features using the appropriate stored procedures or SSMS wizards. Finally, if all else fails, restart the entire server during a maintenance window, then retry the drop. Do not delete files manually until the database is detached or dropped.
Frequently Asked Questions
What causes SQL Server error 1406?
It occurs when SQL Server cannot safely force the removal of a database because it is still in use, or the service cannot be stopped cleanly. Active connections, jobs, or a stuck service state are common triggers.
Can I drop a database that is in single-user mode?
Yes, but you must be the single connected user. Use ALTER DATABASE ... SET SINGLE_USER WITH ROLLBACK IMMEDIATE to force other connections out, then drop.
Is it safe to use WITH ROLLBACK IMMEDIATE?
It rolls back open transactions and terminates connections, which can cause applications to lose work. Use it only when you are sure no critical transactions are in progress, and preferably during a maintenance window.
What if the database is part of an availability group?
You must remove it from the availability group first. Dropping a database that is still in an availability group will fail. Use SSMS or T-SQL to remove it from the group, then drop.
Why does the error mention “force service”?
The message is a generic indicator that SQL Server attempted to stop the service or force removal but could not do so safely. It usually points to a service that did not stop or a database that is still active.
Can I just delete the .mdf and .ldf files?
No. Deleting files while the database is attached corrupts the instance. Always detach or drop through SQL Server first, then delete files if needed.