4

For a project, I'm using a SQL Server 2008 R2. One table has a filestream column.

I've made some load tests, and now the database has ~20GB used.

I've empty tables, except several(configuration tables). But my database was still using a lot of space. So I used the Task -> Shrink -> Database / Files But my database is still using something like 16GB.

I found that it's the filestream file is still using a lot of space.

The problem is that I need to backup this database to export it on the final production server, and event if I indicate to compress the backup I got a file more than 3.5Go. Not convenient to store and upload.

And I'm planning much bigger test, so I want to know how to shrink that empty space.

When I'm trying: enter image description here

I get this exception: enter image description here

The properties SIZE, MAXSIZE, or FILEGROWTH cannot be specified for the FILESTREAM data
file 'FileStreamFile'. (Microsoft SQL Server, Error: 5509)

So what should I do?

I found several topics with this error but they was about removing the filestream column.

2 Answers 2

3

Old versions of files are removed from filestream via a garabage collection process that runs during the checkpoint process.

see http://sqlskills.com/BLOGS/PAUL/post/FILESTREAM-garbage-collection.aspx for a full explanation.

so - you jump through all the hoops, run a log backup, checkpoint and then you... wait, because the stupid garbage collector only seems to delete files out at the rate of about 4 or 5 per second.

new in 2012, I think? is sp_filestream_force_garbage_collection ( http://msdn.microsoft.com/en-us/library/gg492195.aspx ) - but I haven't used it so I can't say how effective it is.

3
  • I'm sorry, this project has been paused for months. So I did a backup, a checkpoint, something like 2 hours ago, but the space is still not released, and the shrink file still doesn't work. What should I do?
    – J4N
    Dec 10, 2012 at 13:51
  • 1
    what kind of backup did you do, you need to remove all references to the file before it will garbage collect it. depending on your recovery mode, a full backup is not enough, you also need to do a transaction backup as well.
    – bkr
    Mar 22, 2013 at 18:39
  • 1
    sp_filestream_force_garbage_collection worked perfectly, and quick too! Cheers. Instantly freed up 130GB+ of garbage files. (You might possibly need to switch to Simple recovery model for this to work.)
    – Kris
    Oct 2, 2018 at 18:18
0

The problem is that the space won't free up until garbage collection is completed. Unfortunately there's no way force this.

I used a powershell script to iterate through the filestream files and empty their content. (note that this will remove the filestream data from all records, as long as you don't need it). This will leave the file that associated to the record in place, but will look like the field was empty.

You will need to change the path to your filesteam folder, but this is essentially what I used.

$files = Get-ChildItem -Path "C:\MSSQL\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\MyDbRestore_9.MyDbRestore_Filestream" -Recurse -File -Exclude "*.hdr"

ForEach ($file in $files) { Clear-Content $file.FullName }
4
  • I still get The file cannot be removed because it is not empty. if I then try to remove the file :( Oct 8, 2021 at 8:38
  • Removing the files at the deepest levels seemed to unblock this Oct 8, 2021 at 8:41
  • Maybe your DB is still active and repopulating the files? Oct 18, 2021 at 18:48
  • Or, maybe your files are in a different location. Oct 18, 2021 at 18:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .