Troubleshooting Guide: SQL Server Error 3222 — “WriteFileEx System Function Only Wrote N Bytes”
Why This Error Happens
This error occurs when SQL Server 2025 attempts an asynchronous write operation to a database or transaction log file, but the underlying Windows I/O subsystem only partially completes the write due to disk-level interference. The most common root causes are antivirus or backup software intercepting the file handle, disk space exhaustion on the volume, or a failing/corrupted storage controller that cannot sustain the requested write size. In virtualized or SAN-based environments, this can also stem from misconfigured storage timeout settings or an unstable network path to the storage array.
Step-by-Step Resolution Guide
Step 1: Exclude SQL Server Files from Antivirus and Backup Agents
- Open your antivirus/EDR console and add exclusions for
*.mdf,*.ldf,*.ndf, and the SQL Server binary directory (typicallyC:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\). - Disable any third-party VSS-based backup agents during active SQL Server backup windows to prevent file-handle contention.
- Restart the SQL Server service after applying exclusions:
Restart-Service MSSQLSERVER.
Step 2: Verify Disk Space and Volume Health
- Run
Get-PSDrive -PSProvider FileSystemin PowerShell to confirm the target volume has sufficient free space (SQL Server needs headroom for log/data file growth). - Use
chkdsk D: /f /r(replaceD:with your data drive) during a maintenance window to scan for and repair file system corruption. - Check Windows Server 2025 Storage Spaces or SAN multipathing (MPIO) health via
Get-PhysicalDiskto rule out a degraded disk.
Step 3: Review and Adjust Storage I/O Timeout Settings
- Navigate to
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Diskand confirm theTimeOutValueis appropriately set (typically 60 seconds) for SAN-attached storage. - If using iSCSI, validate the initiator timeout settings in the iSCSI Initiator Properties under the “Advanced” tab.
- Coordinate with your storage vendor to confirm firmware and driver versions are current, especially after any recent Windows Server 2025 cumulative updates.
Step 4: Check SQL Server Error Log and Event Viewer for Correlating Events
- Open SQL Server Management Studio (SSMS) and review
SQL Server Logsfor repeated 3222 entries and their exact timestamps. - Cross-reference Windows Event Viewer (
ApplicationandSystemlogs) for Disk (Event ID 51/153) or Ntfs warnings occurring at the same time. - If correlating hardware errors are found, escalate to your storage/hardware team before resuming production workloads.
Alternative or Verification Steps
- Run a manual full backup (
BACKUP DATABASE [YourDB] TO DISK = 'D:\Backup\test.bak') and monitor for successful completion without 3222 errors in the SQL error log. - If the issue persists after storage and AV remediation, move the affected database files to a different physical volume or LUN as a temporary workaround while hardware diagnostics continue.