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

Troubleshooting Guide: SQL Server Error 5033 — “The maximum of %ld files per database has been exceeded”

Why This Error Happens

SQL Server 2025 enforces a hard architectural limit of 32,767 files per database (data and log files combined), and this error triggers when a database attempts to exceed that ceiling. This typically occurs due to excessive filegroup fragmentation, automated scripts that repeatedly add files without cleanup, or a corrupted system catalog (sys.database_files) that miscounts existing file entries. In rare cases, replication or Always On Availability Group metadata synchronization issues can cause phantom file entries that count against this limit without being visibly usable.

Step-by-Step Resolution Guide

Step 1: Audit the Current File Count

  • Connect via SQL Server Management Studio (SSMS) or sqlcmd on your Windows Server 2025 instance.
  • Run the following query to inspect existing files:
  SELECT COUNT(*) AS TotalFiles, type_desc 
  FROM sys.database_files 
  GROUP BY type_desc;
  • Cross-reference this against SELECT COUNT(*) FROM sys.master_files WHERE database_id = DB_ID('YourDBName'); to confirm consistency between catalogs.

Step 2: Remove Orphaned or Unused Files

  • Identify empty or unnecessary secondary data files using:
  DBCC SHRINKFILE (N'LogicalFileName', EMPTYFILE);
  • Once confirmed empty, remove the file safely:
  ALTER DATABASE YourDBName REMOVE FILE LogicalFileName;
  • Avoid scripts or ORM migrations that auto-generate new file(s) on every schema deployment — audit any CI/CD pipelines publishing schema changes.

Step 3: Consolidate Filegroups

  • Instead of continuously adding new files, merge underutilized filegroups into fewer, larger files.
  • Use sp_helpfile to list current file allocations and move data using ALTER DATABASE ... MODIFY FILEGROUP to redistribute objects onto consolidated containers.
  • For Windows Server 2025 environments using Storage Spaces Direct (S2D), ensure your volume block size supports larger file growth increments, reducing the need for multiple small files.

Step 4: Rebuild Corrupted System Metadata (If Count Mismatch Persists)

  • Run DBCC CHECKDB (YourDBName) WITH DATA_PURITY; to validate catalog integrity.
  • If mismatches in file metadata persist, restore the database from a known-good backup or use sp_detach_db / sp_attach_db to force SQL Server to rebuild the internal file catalog reference.

Alternative or Verification Steps

  • After remediation, re-run SELECT COUNT(*) FROM sys.database_files; and confirm the total is safely below 32,767 — attempt a test ALTER DATABASE ADD FILE operation to confirm the error no longer triggers.
  • If the issue persists despite a low file count, check for Windows Server 2025 Group Policy restrictions or antivirus/EDR software (e.g., Windows Defender ATP) that may be locking or duplicating .mdf/.ldf handles, causing SQL Server to miscalculate active file entries.