views:

419

answers:

3

With SQL Server 2008 SP1, I've removed the only table that had a Filestream associated with it but each time I attempt to do the following:

alter database ConsumerMarketingStore remove file CMS_JobInstanceFiles

alter database ConsumerMarketingStore remove filegroup JobInstanceFiles

I get the following exception:

Msg 5042, Level 16, State 10, Line 2 The file 'CMS_JobInstanceFiles' cannot be removed because it is not empty. Msg 5042, Level 16, State 11, Line 3 The filegroup 'JobInstanceFiles' cannot be removed because it is not empty.

How in the world do I get rid of the Filestream file and filegroup? Thanks!

A: 

You have to run DBCC SHRINKFILE (CMS_JobInstanceFiles, EMPTYFILE)

This will flag the file as "empty" and allow it to be dropped.

Of course, ALTER DATABASE does not mention this, but DBCC SHRINKFILE does... obvious, eh?

gbn
I actually tried that, you get the following exception:The properties SIZE, MAXSIZE, or FILEGROWTH cannot be specified for the FILESTREAM data file 'CMS_JobInstanceFiles'
James Alexander
@James Alexander: Which command please? DBCC or ALTER DATABASE?
gbn
I believe DBCC SHRINKFILE doesn't work with Filestream files.
Pawel Marciniak
@pave_m: good to know. DBCC does not say that, neither does ALTER...
gbn
+3  A: 

Make sure the table you dropped is in fact the only table that's using that filestream file:

select * from ConsumerMarketingStore.sys.tables t join ConsumerMarketingStore.sys.data_spaces ds on t.filestream_data_space_id = ds.data_space_id and ds.name = 'JobInstanceFiles'

The result of the above query should be empty. If you had other tables with Filestream columns and say you dropped the columns, the table will still use the Filestream file. The way to get rid of this usage is to set table's Filestream filegroup to NULL:

alter table t1 set (filestream_on = "NULL")
Pawel Marciniak
You are an amazing man. After days of MS forums, talking to folks at MS, and SO, you're the first one to provide something useful and the answer. I had a table that at one point did have a file column but I removed it. That was why I was running into issues. Thank you so much.
James Alexander
Exactly what I needed too, thanks.
Sam
A: 

After you drop the table, the garbage collector takes a while to clean up the data. That could be the reason for this happening. You can force garbage collection by issuing a CHECKPOINT.

You can verify whether the FILESTREAM data is cleaned up by going to the Filestream data container. If the FILESTREAM data is deleted an "ALTER DATABASE dbname REMOVE FILE" will usually succeed. Once it is done, you can issue "ALTER DATABASE dbname REMOVE FILEGROUP".

Jacob
Jacob, AFAIK the data need not to be garbage collected in order to drop the filestream file. What's necessary is that the file is not being used by any table (see my reply).Also, note that CHECKPOINT will force GC only under a simple recovery model and it will still only force a single invocation of GC, which may not collect all eligible files.
Pawel Marciniak

related questions