Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FILESTREAM files being left behind after row deleted

Tags:

I have successfully set up FILESTREAM on my SQL 2008 server; however I've noticed that even when I have deleted rows containing FILESTREAM data, the physical data file doesn't seem to get deleted.

By the physical file, I mean the file in SQLServer's managed directory with a uniqueidentifer as the filename not the original file added to the dbase.

Does anyone know if SQLServer will delete the file eventually? If there are a lot of large files removed from the dbase I'd expect to be able to reclaim the space quickly that's all.

like image 334
Dale Avatar asked Oct 26 '09 14:10

Dale


People also ask

What is the difference between Filestream and FileTable?

The FileStream feature stores unstructured data in the file system and keeps a pointer of the data in the database whereas FileTable extends this feature even further to allow non-transactional access (the ability to access files without prior authorization from the Database Engine from the shared location).

What are the isolation semantics for Filestream storage?

The isolation semantics are governed by Database Engine transaction isolation levels. Read-committed isolation level is supported for Transact-SQL and file system access. Repeatable read operations, serializable and snapshot isolation levels are supported. Dirty read isn't supported.

How do I know if Filestream is enabled?

To enable and change FILESTREAM settingsRight-click the instance, and then click Properties. In the SQL Server Properties dialog box, click the FILESTREAM tab. Select the Enable FILESTREAM for Transact-SQL access check box.


2 Answers

FILESTREAM data is subject to transaction control and therefore is not deleted instantly.

Instead, SQL Server runs a garbage collector which purges the old data when it is sure it had been ultimately deleted.

From the documentation:

FILESTREAM garbage collection is a background task that is triggered by the database checkpoint process. A checkpoint is automatically run when enough transaction log has been generated. For more information, see the SQL Server 2008 Books Online topic “CHECKPOINT and the Active Portion of the Log” (http://msdn.microsoft.com/en-us/library/ms189573.aspx). Given that FILESTREAM file operations are minimally logged in the database’s transaction log, it may take a while before the number of transaction log records generated triggers a checkpoint process and garbage collection occurs. If this becomes a problem, you can force garbage collection by using the CHECKPOINTstatement.

like image 140
Quassnoi Avatar answered Oct 01 '22 23:10

Quassnoi


use

sp_filestream_force_garbage_collection 

unfortunately this only works >= SQL Server 2012

like image 40
Michael Sander Avatar answered Oct 01 '22 21:10

Michael Sander