Database File movement in SQL AlwaysOn Availability group: A walk-through

Hello all,


In this blog, we are covering the scenario of moving the database files of AlwaysOn Availability Group (AG) enabled database to another location on all replicas with minimum downtime.

To demonstrate database file movement, using Windows 2012 R2 cluster with AlwaysOn configured on SQL 2016 instance. This walk-through provides an example of how to move the database files to another location. The names and numbers of servers used are for example purposes only.

Primary Replica Node 1: SQLSERVER-0

Secondary Replica Node 2: SQLSERVER-1

Database Name: AutoHA-sample

Availability Group name: Contoso-ag


As per the below screenshots, AutoHa-sample database files are  present at F:\Data and F:\Log directory respectively.




For demonstration , let’s move the data and Log files to another location: "C:\MSSQL13.MSSQLSERVER\MSSQL\Data" .

We will use “Remove Secondary database from the Availability group” approach to achieve this and once the database files are moved to the new location, we will manually add the database back to the AG.

First step is to ensure that the Log backups are disabled on all primary/secondary replicas. This critical step ensures that Log file is not truncated. Since we are using Remove Database Out of AG/Join database to AG approach, log file need to be intact while joining the database back to the AG. If the Log backups continue to happen, then the Transaction log virtual log files are marked for reuse and truncated. Hence join database to AG operation would fail, then the only option available is to manually apply the Log backups/Perform re-initialization with full backup which can be cumbersome.

Note: Since the Log file is not truncated as the Log backups are disabled, ensure that the drive hosting Log file has adequate space.



Next step is to perform the database files movement on the secondary replica. Connect to the secondary replica and suspend the data movement as below:



When the data movement is suspended, the database status in AG changes to "Not- Synchronizing".


Next step is to remove the database out of AG using “Remove Secondary database” option:


Once the database is removed out of AG on the secondary replica, warning sign appears next to the database name in the Availability databases list.


The database state changes to "Restoring" state.


Now that the database is removed from AG, move the database files (Data and Log) to desired location. In this scenario, we are moving the database files to "C:\MSSQL13.MSSQLSERVER\MSSQL\Data" folder.


Since the database is in restoring state on the secondary replica, it is not possible to take the database offline. Next step is to stop the SQL Service on the secondary replica.


Since SQL Service is stopped on the secondary replica, this has no impact on the application. Application will continue connecting to the active primary replica using the AG listener name.

Move the database files to the new location and Start the SQL Service.


The database state will be in restoring state. Executing sysaltfiles view reflects the new file location. Kindly note that sys.database_files/database files through GUI would still reflect the old path till the AG failover is performed.


To join the database back to the availability Group using “Join to Availability group”:


The Log file is intact on the primary replica, AlwaysOn sends the log blocks from last hardened LSN on secondary replica. Transaction Log is replayed on secondary replica, the database is added back to AG.


Review the AG dashboard and wait for the database to come to Synchronized state (Synchronous replica) /Synchronizing (Asynchronous Replica). Time needed to catch up with primary replica depends on several factors like number of Log blocks sent across the network/IO on the secondary replica etc.

Once the database is synchronized, fail over the AG to the secondary replica (which has the database with modified file location).


Once the AG is failed over, perform the same set of steps on the new secondary replica. Enable the Transaction Log backup jobs on all the replicas only post carrying out the file movement activity on all the replicas.

NOTE: As stated earlier, disabling the Log backup jobs is a critical step before starting the file movement activity. Otherwise, while joining the database back to AG, error is reported as per the screenshot below:


Hope the above steps mentioned helps in database file maintenance activity.

Please share your feedback, questions and/or suggestions.


Don Castelino | Premier Field Engineer | Microsoft


Disclaimer: All posts are provided AS IS with no warranties and confer no rights. Additionally, views expressed here are my own and not those of my employer, Microsoft.