Sometimes when SQL database files (.mdf and .ldf files) grow too large, the easiest solution might be to move them to a different location so we can free up disk space quickly.
Here are the steps to move these files:
1. Turn off all services or processes that are using the database2. Note down the file locations of the .mdf and ldf files (by default, they are located at SQL Server application directory e.g., \\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA
123456789 ALTER DATABASE AdventureWorks2014MODIFY FILE ( NAME = AdventureWorks2014_Data,FILENAME = 'E:\New_location\AdventureWorks2014_Data.mdf');GOALTER DATABASE AdventureWorks2014MODIFY FILE ( NAME = AdventureWorks2014_Log,FILENAME = 'E:\New_location\AdventureWorks2014_Log.ldf');GO
12 ALTER DATABASE AdventureWorks2014 SET OFFLINE;GO
12 ALTER DATABASE AdventureWorks2014 SET OFFLINE WITH ROLLBACK IMMEDIATE;GO
12 ALTER DATABASE AdventureWorks2014 SET ONLINE;GO
1. Right click on the SQL Server service e.g., like the one highlighted in the screen shot below
2. Choose Properties option
3. Click Log On tab to change to the ideal user as required
OR
You may also grant access permission to the SQL Server service user on the new folder, where the database files are.
1234 SELECT name, physical_name AS NewLocation, state_desc AS OnlineStatusFROM sys.master_filesWHERE database_id = DB_ID(N'AdventureWorks2014')GO
This article is originally sourced from this one and you may find more details there. Thank you.