🚨 Removing Extra TempDB Data Files in SQL Server (Always On – 2 Node Cluster): Lessons from Production
Recently, I ran into an issue where TempDB had extra data files on a SQL Server Always On Availability Group (AG) configured with two nodes. On the surface, removing unused TempDB files looks straightforward. In reality—especially in AG—it’s anything but simple.
Sharing this post so others don’t repeat the same mistakes I almost did.
🔍 Why do TempDB extra files exist?
Extra TempDB files usually come from:
Over time, these extra files may:
✅ Best practice: TempDB data files = logical CPU cores (up to a reasonable limit), evenly sized.
⚠️ Why is this risky in Always On AG?
Even though TempDB is not part of the AG, the SQL Server instance is.
That means:
So careless execution can cause:
Many DBAs try this:
USE tempdb;
GO
DBCC SHRINKFILE (tempdev_extra_01, EMPTYFILE);
ALTER DATABASE tempdb REMOVE FILE tempdev_extra_01;
Typical errors you’ll encounter:
Error 2555
Cannot move all contents of file 'tempdev_extra_01' because it is not empty.
Error related to worktables
The file 'tempdev_extra_01' cannot be shrunk because it is being used for internal objects.
Sometimes even worse under load:
👉 Why? Because TempDB is continuously used, and SQL Server cannot evacuate the file while internal objects (like worktables, hash spills, version store) are active.
Question: What are the correct methods to handle above situation?
Here are two methods to approach the resolution.
✅ Method 1: Controlled restart + quick execution
This works sometimes but requires discipline.
Steps:
DBCC SHRINKFILE (tempdev_extra_01, EMPTYFILE);
ALTER DATABASE tempdb REMOVE FILE tempdev_extra_01;
Why this works:
Why it still fails sometimes:
So yes—even this is unreliable in busy systems.
✅ Method 2: Start SQL Server in Single‑User Mode (Safest & Most Reliable Option)
When removing extra TempDB files fails even after a restart, starting SQL Server in single‑user mode is the most reliable and controlled approach.
This method works because it completely eliminates competing TempDB usage, which is the root cause of most FILE IN USE / worktable errors.
🔍 What is Single‑User Mode?
Single‑user mode starts SQL Server with:
This gives the DBA exclusive control over TempDB.
⚠️ Important AG Considerations (Very Important)
In an Always On Availability Group (2‑node cluster):
TIP: Always perform TempDB extra file deletion on the secondary replica first. Afterward, fail over and repeat the process on the new secondary replica. This helps avoid unforeseen issues on the primary AG.
✅ Coordinate with:
🧩 Step‑by‑Step Process
1️⃣ Stop SQL Server Agent (Mandatory)
SQL Agent is one of the most common reasons single‑user mode fails, because it grabs the only allowed connection.
Run this in PowerShell-
Stop-Service SQLSERVERAGENT
Or from SQL Server Configuration Manager- STOP sqlserveragent service.
✅ Do NOT skip this step.
2️⃣ Stop SQL Server Service
Run this in PowerShell-
Stop-Service MSSQLSERVER
(Use the named instance service if applicable.)
3️⃣ Start SQL Server in Single‑User Mode
There are two commonly used methods.
✅ Method A: Startup Parameter (Recommended)
4️⃣ Connect Using SQLCMD (Not SSMS)
Use Command Prompt (Run as Admin): Run below command
sqlcmd -S localhost
Recommended by LinkedIn
✅ This ensures you get the only connection.
5️⃣ Remove Extra TempDB Files
Now TempDB is quiet and predictable.
Run below commands in command prompt-
USE tempdb;
GO
DBCC SHRINKFILE (tempdev_extra_01, EMPTYFILE);
GO
ALTER DATABASE tempdb REMOVE FILE tempdev_extra_01;
GO
✔ This step works reliably because:
6️⃣ Exit Single‑User Mode
✅ Method B: Start SQL Server in Single‑User Mode from Command Line (Advanced)
This method is useful when:
This is a more hands‑on approach and should be used carefully.
🔧 Steps to Start SQL Server in Single‑User Mode via Command Prompt
1️⃣ Stop SQL Server Agent (Mandatory)
Again—this is critical.
Run below command in command prompt-
net stop SQLSERVERAGENT
Why we did it? If SQL Agent starts first, it will steal the only allowed connection, locking you out.
2️⃣ Stop SQL Server Service
Run below command in command prompt-
net stop MSSQLSERVER
(For a named instance, use the instance service name.)
3️⃣ Start SQL Server Manually in Single‑User Mode
Open Command Prompt (Run as Administrator) and navigate to the SQL Server Binn directory, for example:
cd "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Binn"
Now start SQL Server with single‑user switch:
sqlservr.exe -m
✅ Optional (BEST PRACTICE):
sqlservr.exe -m"SQLCMD"
This ensures:
📌 The command window will appear “hung” — this is normal. SQL Server is running in the foreground.
4️⃣ Connect Using SQLCMD (Different Window)
Open another Command Prompt (Admin):
sqlcmd -S localhost
✅ If the connection succeeds, you now own the only SQL Server session.
5️⃣ Remove Extra TempDB Files
Now perform the TempDB cleanup safely: Run below in command prompt
USE tempdb;
GO
DBCC SHRINKFILE (tempdev_extra_01, EMPTYFILE);
GO
ALTER DATABASE tempdb REMOVE FILE tempdev_extra_01;
GO
At this point:
This is why this method works when others fail.
6️⃣ Shut Down and Restart Normally
✅ Validation After Startup
Run below in ssms:
SELECT name, physical_name FROM tempdb.sys.database_files;
Confirm:
🚨 Common Pitfalls to Avoid
❌ Forgetting to stop SQL Agent
❌ Connecting via SSMS instead of SQLCMD
❌ Monitoring tools auto‑connecting
❌ Running this during peak business hours
❌ Not informing stakeholders
⚠️ Important Warnings (Please Read)
🚨 DISCLAIMER This post is based on real production experience—but every environment is different.
Before attempting this in PROD:
👉 Do more research, test thoroughly, and proceed with extreme caution.
🧠 Final takeaway
Removing extra TempDB files is not just a storage task— in Always On AG, it’s:
✅ Choose the approach based on risk appetite, not convenience.
If this saves even one DBA from a production scare—that’s a win.
#SQLServer #AlwaysOn #TempDB #DBAExperience #ProductionLessons #HighAvailability #DataPlatform
Senior Oracle & MSSQL Database Administrator OCP, MS-SQL Certified, Azure cloud, oracle cloud certified
3dNice article