By: Greg Robidoux | Updated: 2018-05-29 | Comments (2) | Backup
In a previous tip we looked at how to put together a CLR function for sorting text data. In addition, we have also written tips about how to mimic the functionality of maintenance plans without having to use a maintenance plan. In one of these previous tips, "Maintenance task to delete old backup files" we outlined how to delete older backup files by using a VB Script. To take this deleting of older files a step further, this tip will look at this same task to remove older backup and log files, but this time using a CLR function.
Before we get started the first thing that needs to be done is that you need to enable the CLR on your SQL Server. This can be done by using the code below.
sp_configure 'show advanced options', 1; GO RECONFIGURE; GO sp_configure 'clr enabled', 1; GO RECONFIGURE; GO
In this CLR function we are going to pass in a few parameters such as:
- File path
- Days to keep files
- File Extension
and return a count of the number of files that were deleted as well as delete the actual files.
Step 1 - CLR code
The first thing we need to do is to write the CLR code for this. This could be written in either C#.NET or VB.NET. In this example we are using VB.NET.
The following code has a Class (CLRFunctions) and a Function (DeleteFiles). The function takes three parameters and returns an integer value.
Copy and save the code below in a file called: C:\CLRFunctions.vb
Imports System.IO Public Class CLRFunctions Public Shared Function DeleteFiles(sPath As String, iDaysToKeep As Integer, sFileExtension As String) As Integer Dim arrFiles As Array Dim dateToday As Date Dim myFileInfo As FileInfo Dim myDirectoryInfo As DirectoryInfo Dim iFileCount As Integer Try iFileCount = 0 myDirectoryInfo = New DirectoryInfo(sPath) arrFiles = myDirectoryInfo.GetFiles() dateToday = DateAdd("d", -iDaysToKeep, Today) For Each myFileInfo In arrFiles If myFileInfo.Extension = sFileExtension And myFileInfo.LastWriteTime < dateToday Then myFileInfo.Delete() iFileCount = iFileCount + 1 End If Next Return iFileCount Catch Return 0 End Try End Function End Class
Step 2 - Compile CLR Code
In order to use this code, the code has to be compiled first.
The following command is run from a command line to compile the CLR code using the vbc.exe application. This is found in the .NET 2.0 framework directory. I also tried .NET 4.0 framework and this worked as well. The path may be different on your server or desktop. Also, this code should be compiled on the machine where the code will run.
So from a command line run a command such as the following:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc /target:library C:\CLRFunctions.vb
The code should now be compiled in a file called: C:\CLRFunctions.dll
Step 3 - Create Assembly and Function
After the code has been compiled you need to create the assembly and the function with SQL Server. To do this, run these commands in the database where you want to use the function.
The assembly ties an internal object to the external DLL that was created and the function is similar to a normal SQL Server function.
For the function you will see three components that are referenced CLRFunctions.CLRFunctions.DeleteFiles.
- CLRFunctions - the assembly reference
- CLRFunctions - the class reference in the VB code
- DeleteFiles - the function reference in the VB code
CREATE ASSEMBLY CLRFunctions FROM 'C:\CLRFunctions.dll' WITH PERMISSION_SET = UNSAFE GO CREATE FUNCTION dbo.DeleteFiles ( @FolderPath AS NVARCHAR(100), @DaysToKeep AS integer, @FileExtension AS NVARCHAR(50) ) RETURNS integer AS EXTERNAL NAME CLRFunctions.CLRFunctions.DeleteFiles GO
If you get error messages when trying to compile the code you may need to alter the database using the following command and then try again to create the assembly and the function.
ALTER DATABASE test1 SET trustworthy ON
Step 4 - Test It
To test the function, run the following SELECT statement based on the values you want to pass into the function. Note the file extension is case sensitive.
In this example we are deleting files in the "C:\Backups" folder that are 14 days or older and have an extension of ".BAK"
SELECT dbo.DeleteFiles('C:\Backups', 14, '.BAK') AS FilesDeleted
In this example we are deleting files in the "C:\Backups" folder that are 7 days or older and have an extension of ".trn"
SELECT dbo.DeleteFiles('C:\Backups', 7, '.trn') AS FilesDeleted
In this example we are deleting files in the "C:\Backups" folder that are 14 days or older and have an extension of ".LOG"
SELECT dbo.DeleteFiles('C:\Backups', 14, '.LOG') AS FilesDeleted
Step 5 - Cleanup
To get rid of the code you will need to delete the DLL that is created from the compile step as well as the VB file that was created.
In addition, run this T-SQL code to drop the objects that were created.
DROP FUNCTION dbo.DeleteFiles GO DROP ASSEMBLY CLRFunctions GO
That's all there is to creating a CLR function to delete older backup files. This function as built only looks in one directory as well as only deals with one type of file extension at a time, so these are some things that could be done to improve this code.
One thing to note is that when the files are deleted they are not put into the recycle bin, so make sure you test this in a test environment to make sure you understand what is occurring before you implement this on your production servers.
This code has been tested successfully with SQL Server 2017, so should work fine with all versions that support CLR.
- Give this example a try and see what other functions you could write that could take advantage of the CLR
- If you have some enhancements to this function that you would like to share, please post them below.
- If you don't know how to write either VB or C# now is the time to begin learning.
Last Updated: 2018-05-29
About the author
View all my tips