FILESTREAM files being left behind after row deleted

有些话、适合烂在心里 提交于 2019-12-04 22:33:50

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.

use

sp_filestream_force_garbage_collection

unfortunately this only works >= SQL Server 2012

DELETE FROM tbl_XXX DECLARE @test CHECKPOINT @test = 0

Run this in your sql server and You can observe the file getting deleted from file system also..

You can set the the number of minutes or seconds to wait for the garbage collector to clean up the files from filesystem after performing the deletion operation.

Thanks

haranath

First you have to create a Checkpoint for the Garbage Collector work. After you Deleted rows you can run this code to eliminate all files that don't belong to any row.

USE [DataBaseName]
GO

-- Create a checkpoint on current database
CHECKPOINT
GO 

-- Execute Garbage Collector after a checkpoint created
EXEC sp_filestream_force_garbage_collection  'DataBaseName'
GO
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!