What do I do if databases cannot be directly deleted from an ApsaraDB RDS for SQL Server instance?

更新时间:
复制 MD 格式

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.

Note

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.

ConditionSQL Server versionHA technologyError message
No active sessions2008, 2012, 2016Database mirroringThe database 'XX' is enabled for database mirroring. Database mirroring must be removed before you drop the database.
No active sessions2017Always OnThe 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 exist2008, 2012, 2016, 2017Database mirroring or Always OnCannot 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):

  1. Get the process IDs (PIDs) of all active sessions in the database:

    USE master;
    EXEC sp_who;

    Look for rows where dbname matches your target database. Note the spid value for each active session.

  2. Terminate each active session. Repeat for every session PID you identified:

    -- Replace <spid> with the session PID from the sp_who output
    KILL <spid>;
  3. Remove database mirroring:

    -- Replace <database-name> with the name of the database to drop
    ALTER DATABASE <database-name> SET PARTNER OFF;
  4. Drop the database:

    DROP DATABASE <database-name>;

If no active sessions exist (mirroring error only):

  1. Remove database mirroring:

    ALTER DATABASE <database-name> SET PARTNER OFF;
  2. Drop the database:

    DROP DATABASE <database-name>;

Next steps