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

Why This Error Happens

Error 5013 occurs because SQL Server 2025 explicitly blocks ALTER DATABASE ADD FILE operations against the master and model system databases to protect core engine stability. This is typically triggered when a maintenance script, migration tool, or automated job mistakenly targets master/model instead of a user database, or when a template-based deployment process assumes model can be extended with new filegroups. In rare cases, this also surfaces if a corrupted or misconfigured startup script attempts to replicate file structures from a user database onto model during instance initialization.

Step-by-Step Resolution Guide

Step 1: Identify the Source of the ALTER DATABASE Command

  • Check SQL Server Agent Job history and any CI/CD deployment scripts (Azure DevOps, Flyway, Liquibase, etc.) for ALTER DATABASE master or ALTER DATABASE model statements.
  • Run the following query to review recent DDL attempts via the default trace:
SELECT * FROM sys.fn_trace_gettable(
(SELECT TOP 1 path FROM sys.traces WHERE is_default = 1), DEFAULT)
WHERE EventClass = 164;
  • Confirm whether the target database name was hardcoded incorrectly in a script or configuration file.

Step 2: Correct the Script or Deployment Template

  • Open the offending .sql file or deployment template (commonly found under C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Install\ or your CI pipeline’s artifact folder).
  • Replace references to master or model with the correct target database name (e.g., USE [YourAppDB] before the ALTER DATABASE block).
  • If the script is meant to create a template database, clone model first using CREATE DATABASE NewDB instead of modifying model directly.

Step 3: Validate System Database Integrity

  • Run DBCC CHECKDB (master) WITH NO_INFOMSGS, ALL_ERRORMSGS; to rule out corruption as the underlying trigger.
  • If model was unintentionally altered and is now inconsistent, restore it from the default template backup located at C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Binn\Templates\model.mdf (requires service restart in single-user mode via sqlservr.exe -m).

Step 4: Restart SQL Server Service and Re-test

  • Restart the SQL Server (MSSQLSERVER) service from services.msc or run:
Restart-Service -Name 'MSSQLSERVER' -Force
  • Re-run your original deployment script against the correctly specified user database to confirm Error 5013 no longer appears.

Alternative or Verification Steps

  • Verify the fix by executing sp_helpdb against master and model to confirm no pending file additions remain queued or in a failed state.
  • If the error persists despite correcting the script, check for third-party monitoring/backup agents (e.g., SCOM, Veeam SQL plugins) that may be issuing automated ALTER DATABASE calls against system databases during health-check routines.