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 masterorALTER DATABASE modelstatements. - 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
.sqlfile or deployment template (commonly found underC:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Install\or your CI pipeline’s artifact folder). - Replace references to
masterormodelwith the correct target database name (e.g.,USE [YourAppDB]before theALTER DATABASEblock). - If the script is meant to create a template database, clone
modelfirst usingCREATE DATABASE NewDBinstead of modifyingmodeldirectly.
Step 3: Validate System Database Integrity
- Run
DBCC CHECKDB (master) WITH NO_INFOMSGS, ALL_ERRORMSGS;to rule out corruption as the underlying trigger. - If
modelwas unintentionally altered and is now inconsistent, restore it from the default template backup located atC:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Binn\Templates\model.mdf(requires service restart in single-user mode viasqlservr.exe -m).
Step 4: Restart SQL Server Service and Re-test
- Restart the SQL Server (MSSQLSERVER) service from
services.mscor 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_helpdbagainstmasterandmodelto 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 DATABASEcalls against system databases during health-check routines.