When you run DROP DATABASE on an RDS High-availability Edition instance, SQL Server blocks the operation because the database is still part of a high availability (HA) configuration (database mirroring or Always On availability group) or has active sessions. This topic explains why the error occurs and how to drop the database safely.
Before you perform high-risk operations, such as modifying the configurations or data of Alibaba Cloud instances, we recommend that you check the disaster recovery and fault tolerance capabilities of the instances to ensure data security. Before you modify the configurations or data of an instance, we recommend that you create snapshots or enable backup for the instance. For example, you can enable log backup for an RDS instance. If you granted permissions on sensitive information or submitted sensitive information in the Alibaba Cloud Management Console, we recommend that you modify the sensitive information at the earliest opportunity. Sensitive information includes usernames and passwords.
Prerequisites
Before you begin, make sure that:
Your RDS instance runs SQL Server on RDS High-availability Edition. This procedure does not apply to other editions.
You have the permissions to run stored procedures and DDL statements on the instance.
Understand the errors
The error you see depends on your SQL Server version and whether active sessions exist.
| Condition | SQL Server version | HA technology | Error message |
|---|---|---|---|
| No active sessions | 2008, 2012, 2016 | Database mirroring | The database 'XX' is enabled for database mirroring. Database mirroring must be removed before you drop the database. |
| No active sessions | 2017 | Always On | The database 'XX' is currently joined to an availability group. Before you can drop the database, you need to remove it from the availability group. |
| Active sessions exist | 2008, 2012, 2016, 2017 | Database mirroring or Always On | Cannot drop database "XX" because it is currently in use |
RDS High-availability Edition uses either database mirroring (SQL Server 2008, 2012, 2016) or Always On availability groups (SQL Server 2017) for HA. Before SQL Server lets you drop a database, you must remove it from the HA configuration. If active sessions are connected, you must terminate them first.
Drop the database
SQL Server 2012 and later (recommended)
Run the following stored procedure. It automatically removes the database from the HA configuration, terminates all active sessions, and then drops the database:
-- Replace <database-name> with the name of the database to drop
EXEC sp_rds_drop_database '<database-name>';SQL Server 2008
SQL Server 2008 does not support sp_rds_drop_database. Follow the steps below based on whether active sessions exist.
If active sessions exist (Cannot drop database "XX" because it is currently in use):
Get the process IDs (PIDs) of all active sessions in the database:
USE master; EXEC sp_who;Look for rows where
dbnamematches your target database. Note thespidvalue for each active session.Terminate each active session. Repeat for every session PID you identified:
-- Replace <spid> with the session PID from the sp_who output KILL <spid>;Remove database mirroring:
-- Replace <database-name> with the name of the database to drop ALTER DATABASE <database-name> SET PARTNER OFF;Drop the database:
DROP DATABASE <database-name>;
If no active sessions exist (mirroring error only):
Remove database mirroring:
ALTER DATABASE <database-name> SET PARTNER OFF;Drop the database:
DROP DATABASE <database-name>;
Next steps
To drop a database using the ApsaraDB RDS console or API instead of T-SQL, see Delete a database from an ApsaraDB RDS for SQL Server instance and DeleteDatabase.
For details on the High-availability Edition architecture, see RDS High-availability Edition.