SQL Server error 689, "Operation not allowed because of pending cleanup," appears when you try to drop or alter a database (or sometimes a table) while SQL Server is still finishing a previous cleanup operation on that object. The fix is almost always to wait a few seconds and retry, but if the error persists, you need to find and kill the blocking session or force the cleanup to complete. Below are the exact steps to resolve it, starting with the simplest and moving to more forceful methods.

What You'll Need

  • SQL Server Management Studio (SSMS) or any query tool that can run T-SQL (like Azure Data Studio).
  • Login credentials with sufficient permissions: to drop a database you need ALTER ANY DATABASE or be a member of the sysadmin or dbcreator fixed server roles. To kill a session you need KILL permission, which is held by sysadmin or processadmin roles.
  • Access to the SQL Server error log or the ability to run sp_who2 or query sys.dm_exec_requests to identify blocking sessions.

Step-by-Step

  1. Retry the operation after a short wait. The most common cause is a transient cleanup that completes in a few seconds. Wait 10–30 seconds and run your DROP DATABASE or ALTER DATABASE statement again. If it succeeds, you're done.
  2. Check for active sessions on the database. If the error persists, run this query to see who is connected to the database you're trying to drop or alter:
    SELECT session_id, login_name, status, host_name, program_name
    FROM sys.dm_exec_sessions
    WHERE database_id = DB_ID('YourDatabaseName');

    Replace YourDatabaseName with the actual name. If you see sessions with status running or sleeping, they may be holding locks that prevent cleanup.
  3. Identify the blocking session. Run sp_who2 or query sys.dm_exec_requests to see which session is blocking the cleanup. Look for a session with a wait_type like LCK_M_* or PAGEIOLATCH_*. The blocking session's blocking_session_id column in sys.dm_exec_requests will point to the culprit.
  4. Kill the blocking session (if safe). If you have identified a session that is stuck or no longer needed, run KILL <session_id> (e.g., KILL 52). Be careful: killing a session will roll back any uncommitted transaction in that session. Only kill sessions you are sure are not doing important work. After killing, retry your operation.
  5. Set the database to single-user mode (for DROP DATABASE). If you are trying to drop a database and sessions keep reconnecting, force it into single-user mode with rollback:
    ALTER DATABASE YourDatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
    Then run DROP DATABASE YourDatabaseName; immediately. This disconnects all other sessions and rolls back their transactions, which clears the pending cleanup. Note: this is disruptive—anyone using the database will be disconnected.
  6. For ALTER DATABASE operations, check for pending checkpoints or log backups. Sometimes the cleanup is waiting on a checkpoint or a log backup. Run CHECKPOINT in the database (you need to be in that database context) and then retry. Also ensure no log backup is currently running.
  7. Restart SQL Server (last resort). If nothing else works, restart the SQL Server service. This forces all cleanup operations to complete. On a production server, do this during a maintenance window. After restart, retry your operation.

Common Mistakes

  • Killing the wrong session. Killing a session that is actively running a critical transaction can cause data loss or corruption. Always verify the session's context (what query it's running) before killing.
  • Not checking for replication or mirroring. If the database is part of replication or had a database mirroring/Always On availability group, the cleanup may be waiting for those components. Check sys.dm_hadr_availability_group_states or replication jobs before assuming it's a simple blocking issue.
  • Using SINGLE_USER without ROLLBACK IMMEDIATE. If you just set single-user without the rollback, the command will hang if other sessions are active. Always include WITH ROLLBACK IMMEDIATE to force disconnect.
  • Forgetting to switch back to multi-user. If you set a database to single-user and then don't drop it, remember to set it back to multi-user with ALTER DATABASE YourDatabaseName SET MULTI_USER; otherwise users can't connect.

Tips

  • Run cleanup during low activity. The error is more likely when many users are connected. Schedule database drops or alterations during maintenance windows.
  • Use a script to automate the retry. You can write a simple loop in T-SQL that retries the drop every 5 seconds up to 10 times. This handles transient cleanup delays gracefully.
  • Check the SQL Server error log. Look for messages like "Cleanup of database 'X' is in progress" or "Error: 689" in the log. This can give more context on what exactly is pending.
  • Keep your backups current. Before any drop or major alteration, take a full backup. If something goes wrong, you can restore.

Troubleshooting

If the error still appears after following the steps above, try these additional checks:

  • Check for orphaned sessions. Sometimes a session from a previous connection is stuck in a state that doesn't show up in sys.dm_exec_sessions but still holds a reference. Run DBCC OPENTRAN to see any open transactions that might be blocking cleanup.
  • Look at the SQL Server error log for the exact cleanup operation. Run EXEC sp_readerrorlog 0, 1, '689' to see the most recent error 689 entries. This may indicate which database and what operation was pending.
  • Check for delayed durability. If the database has delayed durability enabled (a database setting), cleanup might wait for transaction log writes to be flushed. You can disable delayed durability by setting DELAYED_DURABILITY = DISABLED on the database, but this is a permanent change that affects performance.
  • If the database is in a failover cluster or availability group, check the health of the other replicas. The cleanup might be waiting for a secondary replica to acknowledge a log block. Use sys.dm_hadr_database_replica_states to check synchronization states.
  • As a last resort, stop and start the SQL Server service. This is the nuclear option. Before doing this, make sure you have no active users and have a maintenance window. After restart, the pending cleanup should be gone.

Frequently Asked Questions

What exactly causes error 689?

Error 689 occurs when SQL Server is still performing an internal cleanup operation on a database (like removing old versions of pages or releasing memory) and you attempt to drop or alter that database. The operation is blocked until the cleanup finishes.

How long does the pending cleanup usually last?

It varies. For a small database, it may be a few seconds. For a large database with heavy transaction activity, it could take minutes. If it lasts longer than 10 minutes, something is likely stuck.

Can I prevent this error from happening?

Yes, by avoiding dropping or altering databases during peak usage. Also, ensure no long-running transactions are active when you attempt the operation. You can also set a timeout on your command so it fails gracefully instead of hanging.

Is it safe to kill a session to resolve this?

Only if you are certain the session is not doing important work. Killing a session rolls back its transaction, which can cause data loss if the transaction was partially committed. Always verify the session's last query and status first.

Does this error happen only with DROP DATABASE?

No, it can also happen with ALTER DATABASE (like changing recovery model or setting options), and occasionally with DROP TABLE if the table is large and has pending cleanup. The steps are similar.

What if I don't have permission to kill sessions?

Contact your SQL Server administrator. They can run the KILL command or perform the restart. If you are the admin, ensure you have the processadmin role.