Skip to content
Microsoft SQL Server

SQL Server Error 1834

The file cannot be overwritten because it is being used by another database

Severity 16 Backup & Restore
Applies to: All supported SQL Server versions (2016 to 2025), Azure SQL Managed Instance
Last technically reviewed 24 October 2026 on SQL Server 2025 (17.0.1145.4)
The full message
Msg 1834, Level 16, State 1, Line 1
The file 'D:\SQLData\AppDb.mdf' cannot be overwritten.  It is being used by database 'AppDb'.
Other forms of this message
Follows each 1834 (reproduced)
Msg 3156, Level 16, State 4, Line 1
File 'AppDb' cannot be restored to 'D:\SQLData\AppDb.mdf'. Use WITH MOVE to identify a valid location for the file.
Ends the statement (reproduced)
Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
Error number
1834
Severity
16 (user-correctable error)
State
1
Product
Microsoft SQL Server Database Engine
Versions
All supported SQL Server versions (2016 to 2025), Azure SQL Managed Instance
Last reviewed
24 October 2026

What it means

Error 1834 means a RESTORE would write a database file to a path that another attached database is using, so SQL Server refuses before touching anything.

A backup remembers the full path of every file it came from. Unless you say otherwise with MOVE, RESTORE puts each file back at that path. When the path already belongs to a database that is attached to the instance, SQL Server raises 1834 for that file. The message names both sides: the file path in the first quotes, and in the second quotes the database that owns it now, which is usually not the database you are restoring.

You normally see one 1834 per file (data and log), each followed by error 3156 ("Use WITH MOVE"), then 3119 and 3013. WITH REPLACE does not help: REPLACE lets RESTORE overwrite the target database and its own files, never the files of a different database that is online. The safe way out is to give every file a path that nothing else uses.

Common causes

  • Restoring a copy of a database under a new name on the same instance as the original, without MOVE: the files default to the original database's paths.
  • Refreshing a test database from a production backup taken on the same server, where WITH REPLACE was added but the files were not moved to the test database's own paths.
  • A restore script with MOVE paths copied from another database, so they point at a database that is still attached (for example a dev copy).
  • The SSMS restore dialog keeping the original file names in the Files page when the database name is changed.

Diagnose it

Read-only These queries only read metadata and dynamic management views. They do not change data, permissions or configuration, so they are safe to run first.

Which database owns the path in the message?

SELECT DB_NAME(database_id) AS database_name, name AS logical_name,
       type_desc, physical_name, state_desc
FROM sys.master_files
WHERE physical_name LIKE N'%AppDb%';   -- part of the path from the message
What to look for

The database and logical file that use the path. That database must keep its files; your restore needs different paths.

Which files and paths does the backup contain?

Run RESTORE FILELISTONLY FROM DISK = N'D:\Backups\AppDb.bak'; to read the logical names and original paths from the backup file (it reads only the header). For a backup taken on this server, the history shows the same:

SELECT TOP (20) bs.database_name, bs.backup_finish_date,
       bf.logical_name, bf.file_type, bf.physical_name
FROM msdb.dbo.backupset AS bs
JOIN msdb.dbo.backupfile AS bf ON bf.backup_set_id = bs.backup_set_id
ORDER BY bs.backup_finish_date DESC, bf.file_type;
What to look for

The logical names to use in MOVE (file_type D is data, L is log) and the original paths that collide with the attached database.

Where should new files go?

SELECT SERVERPROPERTY('InstanceDefaultDataPath') AS default_data_path,
       SERVERPROPERTY('InstanceDefaultLogPath')  AS default_log_path;
What to look for

The default folders for data and log files. Build new, unique file names there (for example after the new database name).

How to fix it

Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.

Restore with MOVE to new, unique file names

Changes database state

The usual fix. Use the logical names from RESTORE FILELISTONLY and file names that no database uses, typically based on the new database name. The original database is not touched.

RESTORE DATABASE AppDb_Copy
FROM DISK = N'D:\Backups\AppDb.bak'
WITH MOVE N'AppDb'     TO N'D:\SQLData\AppDb_Copy.mdf',
     MOVE N'AppDb_log' TO N'D:\SQLLogs\AppDb_Copy_log.ldf',
     CHECKSUM;

Refreshing an existing database: move to its own files

Changes database state

When you deliberately overwrite a test database, point MOVE at the paths of that test database (diagnostic 1 lists them), not at the source database's paths.

Caution: REPLACE discards the existing target database and everything in it that is not in a backup. Confirm the target name and its file paths before running it, and back it up first if anything may be needed again.

RESTORE DATABASE AppDb_Test
FROM DISK = N'D:\Backups\AppDb.bak'
WITH REPLACE,
     MOVE N'AppDb'     TO N'D:\SQLData\AppDb_Test.mdf',      -- AppDb_Test's own files
     MOVE N'AppDb_log' TO N'D:\SQLLogs\AppDb_Test_log.ldf';

In SSMS, relocate the files before restoring

Client or network side

In the Restore Database dialog, open the Files page, tick "Relocate all files to folder" and check that the "Restore As" names are unique. The dialog generates the same MOVE clauses as the first fix; use Script to review them.

Examples

Restoring a copy under a new name without MOVE

Throwaway database and backup file in the instance default backup folder. The copy is named cws_errlab_copy here; adding WITH REPLACE gives the same output.

USE master;
DECLARE @bak nvarchar(260) = CAST(SERVERPROPERTY('InstanceDefaultBackupPath') AS nvarchar(200)) + N'\cws_errlab_1834a.bak';
BACKUP DATABASE AppDb TO DISK = @bak WITH INIT;

RESTORE DATABASE cws_errlab_copy FROM DISK = @bak;   -- AppDb is still attached
Result
Msg 1834, Level 16, State 1, Line 5
The file 'D:\SQLData\AppDb.mdf' cannot be overwritten.  It is being used by database 'AppDb'.
Msg 3156, Level 16, State 4, Line 5
File 'AppDb' cannot be restored to 'D:\SQLData\AppDb.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 1834, Level 16, State 1, Line 5
The file 'D:\SQLData\AppDb_log.ldf' cannot be overwritten.  It is being used by database 'AppDb'.
Msg 3156, Level 16, State 4, Line 5
File 'AppDb_log' cannot be restored to 'D:\SQLData\AppDb_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 5
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 5
RESTORE DATABASE is terminating abnormally.

Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.

REPLACE with MOVE onto the files of another attached database

Three throwaway databases: the source cws_errlab (logical files cws_errlab and cws_errlab_log), a copy being restored, and cws_errlab_test, which is still attached and whose files the MOVE clauses wrongly target. REPLACE does not allow this; cws_errlab_test is dropped afterwards.

USE master;
CREATE DATABASE cws_errlab_test;   -- another database that is still attached
GO
DECLARE @bak nvarchar(260) = CAST(SERVERPROPERTY('InstanceDefaultBackupPath') AS nvarchar(200)) + N'\cws_errlab_1834b.bak';
DECLARE @mdf nvarchar(260) = (SELECT physical_name FROM sys.master_files WHERE database_id = DB_ID(N'cws_errlab_test') AND type = 0);
DECLARE @ldf nvarchar(260) = (SELECT physical_name FROM sys.master_files WHERE database_id = DB_ID(N'cws_errlab_test') AND type = 1);
BACKUP DATABASE cws_errlab TO DISK = @bak WITH INIT;
RESTORE DATABASE cws_errlab_copy FROM DISK = @bak
WITH REPLACE, MOVE N'cws_errlab' TO @mdf, MOVE N'cws_errlab_log' TO @ldf;   -- cws_errlab_test's files
GO
DROP DATABASE cws_errlab_test;
Result
Msg 1834, Level 16, State 1, Line 5
The file 'D:\SQLData\cws_errlab_test.mdf' cannot be overwritten.  It is being used by database 'cws_errlab_test'.
Msg 3156, Level 16, State 4, Line 5
File 'cws_errlab' cannot be restored to 'D:\SQLData\cws_errlab_test.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 1834, Level 16, State 1, Line 5
The file 'D:\SQLData\cws_errlab_test_log.ldf' cannot be overwritten.  It is being used by database 'cws_errlab_test'.
Msg 3156, Level 16, State 4, Line 5
File 'cws_errlab_log' cannot be restored to 'D:\SQLData\cws_errlab_test_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 5
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 5
RESTORE DATABASE is terminating abnormally.

Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.

How to prevent it

Script restores instead of clicking through the dialog, and always write a MOVE clause for every file with a path built from the target database name. A short naming rule (database name plus .mdf and _log.ldf) makes a collision with another database obvious when you review the script.

Before any restore on a shared instance, list the files in the backup (RESTORE FILELISTONLY) next to sys.master_files. If a backup path appears in both lists, the restore needs MOVE.

Common questions

Why does WITH REPLACE not fix error 1834?

REPLACE allows RESTORE to overwrite the database you are restoring and its files. It never lets you overwrite files that belong to another online database; only MOVE to different paths does.

Can I take the other database offline to free the files?

Technically the files are then no longer in use, but the restore would overwrite that database's data. Only do that if you really intend to destroy it; otherwise use new paths.

Where do I find the logical names for MOVE?

RESTORE FILELISTONLY on the backup file returns them in the LogicalName column, together with the original physical paths.

Learn more on CodeWithSQL

Official documentation

Last technically reviewed 24 October 2026; examples and diagnostics run on SQL Server 2025 (17.0.1145.4). Message text checked against the engine's sys.messages catalogue.

Still stuck on a SQL Server error?

Browse the full SQL Server error library, or get hands-on help from an experienced SQL professional.