question

Matt Whitfield avatar image
Matt Whitfield asked

How do I remove a file from a filegroup?

Seeder question: I have a file in a filegroup which was created in the wrong place, so I need to remove it and add it back in the right place. However, SQL Server tells me that I cannot remove the file because it has data in it. I cannot take the database off-line completely because it is in constant use. How do I go about removing and adding this file in the right place?

t-sqlfilegroupstorage
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kev Riley avatar image
Kev Riley answered

You need to run DBCC SHRINKFILE with the EMPTYFILE clause, to move data from the file into other files in the same filegroup.

SQL will then prevent any more data being written to that file. You will then be able to use ALTER DATABASE to remove that file.

Alternatively, you can do this from SSMS. This MSDN article takes you through the steps

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.