SQL Server Error 1135 occurs when you attempt to restore a database and the system discovers that the existing transaction log file (.ldf) on the destination server is larger than the log file contained within the backup set. This typically happens when a database was grown significantly on the target server before a restore was attempted, or when restoring a backup from a smaller environment to a larger one where a remnant file already exists. To fix this, you must use the WITH REPLACE command or manually delete the existing physical log files from the disk before initiating the restore process.
What You'll Need
- Administrative access to the SQL Server instance (sysadmin role).
- SQL Server Management Studio (SSMS) installed and connected to the instance.
- The .bak backup file you are attempting to restore.
- File system permissions to delete or move files in the SQL Server Data directory.
- Knowledge of the logical file names of the database you are restoring.
Step-by-Step
Depending on whether you prefer a graphical interface or a script, there are two primary ways to resolve Error 1135. Note that menu labels in SSMS may vary slightly depending on the version or service pack installed.
Method 1: Using T-SQL (The Most Reliable Method)
- Identify Logical File Names: Before you can move or replace files, you need to know the logical names stored inside the backup. Open a New Query window in SSMS and run:
RESTORE FILELISTONLY FROM DISK = 'C:\Path\To\Your\Backup.bak';
Look at theLogicalNamecolumn. You will see one entry for the data file (mdf) and one for the log file (ldf). - Construct the Restore Command: Use the
RESTORE DATABASEcommand combined with theMOVEandREPLACEoptions. TheREPLACEflag tells SQL Server to overwrite any existing database with the same name, regardless of file size discrepancies.RESTORE DATABASE [YourDatabaseName] FROM DISK = 'C:\Path\To\Your\Backup.bak' WITH MOVE 'LogicalDataFileName' TO 'C:\SQLData\YourDatabase.mdf', MOVE 'LogicalLogFileName' TO 'C:\SQLData\YourDatabase_log.ldf', REPLACE;
Replace the placeholders with your actual database name, logical names from step 1, and the physical paths where you want the files to reside. - Execute the Script: Press F5 to run the command. The
REPLACEkeyword overrides the size check that triggers Error 1135, forcing the system to accept the smaller log file from the backup.
Method 2: Using SQL Server Management Studio (GUI)
- Initiate Restore: Right-click on the
Databasesfolder in Object Explorer and selectRestore Database.... - Select Source: Choose
Device, click the ellipsis button (...), and add your .bak file. - Configure Files: On the left-hand pane, click the
Filespage. Ensure theRestore Ascolumn points to the correct physical paths. If you are restoring over an existing database, ensure the paths match the current files. - Override the Size Check: Click on the
Optionspage in the left-hand pane. Check the box labeledOverwrite the existing database (WITH REPLACE). This is the critical step that prevents Error 1135. - Close Existing Connections: While still on the
Optionspage, check the boxClose existing connections to destination database. This ensures the restore doesn't fail due to the database being in use. - Complete Restore: Click
OKto begin the process.
Method 3: Manual File Deletion (The Hard Reset)
If the REPLACE command fails because the files are locked or corrupted, you can manually clear the path.
- Detach or Drop the Database: If the database currently exists on the server, right-click it and select
Delete. Ensure you check the boxClose existing connections. - Navigate to Data Folder: Open Windows File Explorer and go to the folder where your .mdf and .ldf files are stored (usually in the
MSSQL\DATAdirectory). - Delete the Log File: Find the specific .ldf file associated with the database you are restoring and delete it. If you are unsure, move it to a backup folder.
- Perform Restore: Return to SSMS and perform the restore as normal. Since the physical file no longer exists on disk, SQL Server will create a new log file based on the size defined in the backup, bypassing the 1135 error.
Common Mistakes
- Forgetting the REPLACE flag: Many users try to restore multiple times without checking the
WITH REPLACEbox in the GUI or adding it to the T-SQL script. Without this, SQL Server will always protect the existing (larger) file from being overwritten by a smaller one. - Incorrect Logical Names: Using the physical file name (e.g., database_log.ldf) instead of the logical name (e.g., YourDB_Log) in the
MOVEcommand. This will result in a different error stating that the logical file was not found in the backup. - Insufficient Disk Space: While Error 1135 is about the file being too large, attempting to move files to a new drive that is nearly full can cause the restore to fail halfway through, leaving the database in a
RESTORINGstate. - Permissions Issues: Attempting to restore to a folder where the SQL Server Service Account does not have write permissions. Always ensure the folder has full control permissions for the account running the
MSSQLSERVERservice.
Tips
- Use T-SQL for Precision: While the GUI is convenient, T-SQL is far more precise for Error 1135 because you can explicitly see the
MOVEandREPLACEcommands. It also provides more detailed error messages if the process fails. - Verify Backup Integrity: Before attempting a restore that requires replacing files, run
RESTORE VERIFYONLY FROM DISK = 'path_to_backup.bak';to ensure the backup file isn't corrupted. - Check Log Growth Settings: After successfully restoring, check the Autogrowth settings of your log file. If the log grew excessively on the previous server, it might be due to a lack of regular log backups or a massive one-time data import.
- Standardize Paths: If you frequently restore databases between different environments (Dev, Test, Prod), try to keep the folder structures identical to minimize the need for the
MOVEcommand.
Troubleshooting
If you have followed the steps above and the restore still fails, consider these scenarios:
The database is stuck in 'Restoring' state: This happens if a restore was interrupted. You cannot delete the files while the database is in this state. Run DROP DATABASE [YourDatabaseName]; to clear the entry from the system catalog, then try the restore again.
Access Denied when deleting .ldf files: If Windows will not let you delete the log file, the SQL Server service may still be holding a handle on it. Restart the SQL Server service via services.msc, then immediately delete the file before restarting the service again.
Error 1135 persists despite REPLACE: This is rare but can happen if there is a mismatch in the version of SQL Server. Ensure you are not trying to restore a backup from a newer version of SQL Server (e.g., 2022) onto an older version (e.g., 2017), as this is not supported and may throw misleading errors.
Logical file names are confusing: If the backup was taken from a database that was renamed multiple times, the logical names may not match the database name. Always rely on RESTORE FILELISTONLY rather than guessing the names.
Frequently Asked Questions
Why does SQL Server care if the log file is larger?
SQL Server implements this check as a safety mechanism to prevent the accidental overwriting of a database that may contain more recent or more extensive transaction data than what is present in the backup file.
Does using WITH REPLACE delete my data?
Yes. Using WITH REPLACE tells SQL Server to discard the existing database and its files entirely and replace them with the contents of the backup. Ensure you have a current backup of the destination database if you need its data.
Can I fix Error 1135 without deleting the database?
No. Because the error is caused by a physical file size mismatch on the disk, you must either overwrite the existing file using REPLACE or delete the physical file so SQL Server can create a new one.
Will this process shrink my log file?
Yes. Since you are replacing the large existing log file with the smaller log file from the backup, the resulting log file will be the size it was at the time the backup was created.
Is it safe to delete the .ldf file manually?
It is safe only if you are performing a full restore from a backup. Never delete a log file of a production database that you intend to keep running, as this will cause the database to enter a SUSPECT or RECOVERY_PENDING state.