Ever wanted to delete files from the local machine that your SQL Server database is running? You can do it using the extended stored procedure xp_cmdshell like this:
EXEC master..xp_cmdshell 'del C:file.txt'
But this requires the sysadmin option on the SQL Server and Admin role fromNT server. In most instances it is not preferable to give these privileges. So to delete files without requiring this access use the built-in SQL Server Automation APIs and the FileSystemObject:
DECLARE @hr intDECLARE @ole_FileSystem intEXEC @hr = sp_OACreate 'Scripting.FileSystemObject', @ole_FileSystem OUTEXEC @hr = sp_OAMethod @ole_FileSystem, 'DeleteFile', NULL, 'C:file.txt'EXEC @hr = sp_OADestroy @ole_FileSystem