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

Troubleshooting Guide: SQL Server Error 3191 – Restore Cannot Continue Because File Cannot Be Written

Why This Error Happens

This error occurs when the SQL Server service account lacks the necessary NTFS write permissions on the destination folder or the target database files themselves. It can also happen if the physical file path specified in the restore command is mapped to a read-only drive, a network share with restricted access, or a location where the files are marked as “Read-Only” at the OS level. Additionally, if another process has locked the file (such as an antivirus scan or a stale SQL process), the restore operation will fail to gain write access.

Step-by-Step Resolution Guide

Step 1: Verify File System Permissions

  • Navigate to the target directory (e.g., C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA) in File Explorer.
  • Right-click the folder, select Properties > Security, and ensure the SQL Server service account (e.g., NT Service\MSSQLSERVER) has Full Control.
  • If missing, click Edit, add the service account, and grant Full Control permissions, then propagate changes to all child objects.

Step 2: Check for Read-Only Attributes

  • Right-click the specific .mdf, .ndf, or .ldf file referenced in the error message.
  • Select Properties and ensure the Read-only checkbox at the bottom is unchecked.
  • Alternatively, run this command in an elevated Command Prompt to clear the attribute:
  attrib -r "C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\YourDatabase.mdf"

Step 3: Validate the Target Path and Drive Status

  • Confirm the destination drive is not mounted as Read-Only (common with mapped network drives or cloud-sync folders like OneDrive).
  • Use WITH MOVE in your RESTORE DATABASE T-SQL command to redirect files to a local, writable path if the original location is inaccessible:
  RESTORE DATABASE [YourDB] 
  FROM DISK = 'C:\Backup\YourDB.bak' 
  WITH MOVE 'YourDB_Data' TO 'D:\SQLData\YourDB.mdf', 
       MOVE 'YourDB_Log' TO 'D:\SQLData\YourDB.ldf';

Step 4: Release File Locks and Restart Services

  • Check if an external process (Antivirus/Backup software) has the file locked using Resource Monitor (resmon.exe) under the CPU tab > Associated Handles.
  • If a lock is found and cannot be cleared, restart the SQL Server (MSSQLSERVER) service via services.msc to release any stale handles before retrying the restore.

Alternative or Verification Steps

  • Verify the Fix: Re-run the RESTORE DATABASE command via SQL Server Management Studio (SSMS) or T-SQL; a successful execution will return the message “RESTORE DATABASE successfully processed.”
  • Alternative Fix: If permissions issues persist on a cluster or Availability Group node, temporarily disable the antivirus real-time scanning on the SQL Data/Log directories, as it frequently intercepts file handles during the restore process.