Troubleshooting Guide: SQL Server Error 5219 – Heap Page Could Not Be Moved
Why This Error Happens
Error 5219 occurs during a heap rebuild or data compaction operation (such as ALTER TABLE ... REBUILD or an index maintenance task) when SQL Server’s Storage Engine cannot relocate a specific heap page to consolidate space. This is typically caused by underlying physical corruption on the page, a conflicting lock held by another active transaction on that page, or an I/O subsystem failure that prevents the page from being read/written during the move operation. In rare cases, this is also triggered by disk-level corruption introduced by antivirus software or storage drivers actively scanning the MDF file during the operation.
Step-by-Step Resolution Guide
Step 1: Verify Database Consistency with DBCC CHECKDB
- Open SQL Server Management Studio (SSMS) or
sqlcmdconnected to the affected instance. - Run the following command to identify the scope of corruption:
DBCC CHECKDB ('YourDatabaseName') WITH NO_INFOMSGS, ALL_ERRORMSGS; - Review the output for allocation errors or specific page IDs matching the
%d:%dreferenced in the original Error 5219 message. - If corruption is confirmed, proceed to restore from a known-good backup rather than attempting a repair on a production system.
Step 2: Check for Active Locks or Blocking Transactions
- Query the Dynamic Management Views (DMVs) to rule out a locking conflict rather than physical corruption:
SELECT * FROM sys.dm_tran_locks WHERE resource_database_id = DB_ID('YourDatabaseName'); - Identify if a long-running transaction is holding an exclusive (X) lock on the specific heap page or table.
- If blocking is found, kill the blocking session ID (SPID) using
KILL <SPID>;and retry the heap rebuild operation.
Step 3: Isolate and Exclude Antivirus/Filter Driver Interference
- Navigate to Windows Security settings on your Windows Server 2025 / Windows 11 host.
- Go to Virus & Threat Protection > Manage Settings > Add or Remove Exclusions.
- Add exclusions for the SQL Server data directory, typically located at
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\. - Restart the SQL Server service via
services.mscor runnet stop MSSQLSERVER && net start MSSQLSERVERin an elevated Command Prompt to clear any locked file handles.
Step 4: Rebuild the Heap Table Offline
- If the table is not actively corrupted but merely locked during the online operation, attempt an offline rebuild:
ALTER TABLE YourTableName REBUILD WITH (ONLINE = OFF); - Monitor the SQL Server Error Log located at
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOGfor recurrence of Event ID 5219 during the rebuild.
Alternative or Verification Steps
- Verification: Re-run
DBCC CHECKDB ('YourDatabaseName') WITH NO_INFOMSGS;after the rebuild; a clean result with zero allocation errors confirms the heap page issue is resolved. - Alternative: If physical corruption is confirmed via Step 1 and no backup is available, use
DBCC CHECKTABLE (N'YourTableName', REPAIR_ALLOW_DATA_LOSS)as a last resort, understanding that this may permanently remove the corrupted row(s) from the heap.