How to fix Error 1801 on Windows Server 2025 / Windows 11

Troubleshooting Guide: SQL Server Error 1801 – “Database Already Exists”

Why This Error Happens

Error 1801 occurs when SQL Server 2025 detects that a database with the specified name is already registered in the system catalog (sys.databases), preventing a duplicate CREATE DATABASE operation. This commonly happens due to orphaned database entries left behind after an incomplete deletion, a database existing in a “Restoring” or “Suspect” state that still occupies the name, or a naming collision when restoring/attaching a database from another server. It can also occur if a previous script failed mid-execution but the database entry was already committed to the master database before the failure.

Step-by-Step Resolution Guide

Step 1: Verify the Database Actually Exists

  • Open SQL Server Management Studio (SSMS) or Azure Data Studio and connect to the instance.
  • Run the following query to check current databases:
  SELECT name, state_desc FROM sys.databases WHERE name = 'YourDatabaseName';
  • Confirm whether the database is ONLINE, RESTORING, RECOVERY_PENDING, or SUSPECT.

Step 2: Remove or Rename the Conflicting Database

  • If the database is in a bad state (e.g., RESTORING or SUSPECT) and no longer needed, drop it:
  DROP DATABASE YourDatabaseName;
  • If the database is needed but you want to create a new one, rename the existing database instead:
  ALTER DATABASE YourDatabaseName MODIFY NAME = YourDatabaseName_Old;
  • If DROP DATABASE fails due to active connections, set it to single-user mode first:
  ALTER DATABASE YourDatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
  DROP DATABASE YourDatabaseName;

Step 3: Check for Orphaned Files on Disk

  • Navigate to the default data directory, typically:
    C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\
  • Look for leftover .mdf or .ldf files matching the database name that may be causing a file-path conflict during creation.
  • If found and not in use, manually delete them (after confirming no backup dependency), or specify a different file path in your CREATE DATABASE statement using the FILENAME parameter.

Step 4: Restart SQL Server Service to Clear Cached Metadata

  • Open Services.msc on Windows Server 2025 / Windows 11.
  • Locate SQL Server (MSSQLSERVER), right-click, and select Restart.
  • This clears any stale metadata cache that might be incorrectly reporting the database as existing.

Step 5: Recreate the Database with a Unique Name

  • If the conflict persists, create the database under a new name to bypass the immediate error:
  CREATE DATABASE YourDatabaseName_New;
  • Once confirmed stable, rename it back if required using ALTER DATABASE ... MODIFY NAME.

Alternative or Verification Steps

  • Run SELECT name FROM sys.databases; after the fix to confirm only one instance of the database name exists and that its state_desc shows ONLINE.
  • If the error persists despite no visible duplicate, check for a mismatch between Azure Data Studio/SSMS object explorer cache and the server state — refresh the Object Explorer or reconnect the session entirely.