Troubleshooting Guide: SQL Server Error 3142 – File Cannot Be Restored Over Existing File
Why This Error Happens
Error 3142 occurs because SQL Server 2025 detects that the database files targeted by your RESTORE operation already exist on disk, either from an existing database or leftover files from a previous failed restore. By default, SQL Server blocks overwriting existing files as a safety mechanism to prevent accidental data loss. This typically happens when restoring a backup to a database name that already exists, or when restoring to a new database whose default file paths collide with files from another database.
Step-by-Step Resolution Guide
Step 1: Identify the Conflicting File Path
- Open SQL Server Management Studio (SSMS) and check the exact file path mentioned in the error message (e.g.,
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\YourDB.mdf). - Run
RESTORE FILELISTONLY FROM DISK = 'C:\Backups\YourBackup.bak';to confirm the logical and physical file names contained in the backup set. - Cross-reference this with
sys.master_filesto see if a database is already using that physical path.
Step 2: Reissue the RESTORE Command with WITH REPLACE
- Add the
REPLACEkeyword to your T-SQL script to explicitly authorize SQL Server to overwrite the existing files. - Example syntax:
RESTORE DATABASE YourDB
FROM DISK = 'C:\Backups\YourBackup.bak'
WITH REPLACE, RECOVERY;
- Ensure no active connections are locked on the target database, as this can cause the restore to hang even with the
REPLACEflag.
Step 3: Move or Delete Orphaned Files (If Replace Fails)
- If
WITH REPLACEfails due to file locks, navigate to the Windows File Explorer path (e.g.,D:\SQLData\). - Stop the SQL Server service via
services.mscor runNET STOP MSSQLSERVERin an elevated Command Prompt. - Manually delete or rename the orphaned
.mdfand.ldffiles blocking the restore, then restart the service withNET START MSSQLSERVER.
Step 4: Redirect File Paths Using MOVE
- If you intend to keep the existing database intact and create a separate copy, use the
MOVEoption instead of overwriting. - Specify new physical paths for the data and log files:
RESTORE DATABASE YourDB_Copy
FROM DISK = 'C:\Backups\YourBackup.bak'
WITH MOVE 'YourDB_Data' TO 'D:\SQLData\YourDB_Copy.mdf',
MOVE 'YourDB_Log' TO 'D:\SQLLogs\YourDB_Copy.ldf',
RECOVERY;
Alternative or Verification Steps
- Verify the fix by checking the SQL Server Error Log (via SSMS > Management > SQL Server Logs) for a “Restore completed successfully” message following the command execution.
- If permissions are suspected as a root cause (rather than existing files), verify that the SQL Server Service Account has Full Control permissions on the destination data/log folder via Windows Security properties.