HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

How is the new system stored procedure sys.xp_delete_files different from sys.xp_delete_file?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
storedthexp_delete_filenewsystemproceduredifferentsyshowxp_delete_files

Problem

SQL Server 2019 introduced a new (and undocumented) system stored procedure:

sys.xp_delete_files

What, exactly, does this new extended stored procedure do? How does it differ from xp_delete_file that has been around for years? Does it replace xp_delete_file?

Solution

To start with, the older xp_delete_file (also undocumented) is quite limited:

  • It can only delete either backup (.bak / .trn) or report (??) files (I believe it actually scans the first part of them to verify the file "type"



  • It only deletes by file extension (i.e. not specific files, or whatever matches based on wildcards) and based on being older than the supplied date



  • It's picky and requires a trailing slash on the directory / folder



  • It can only delete files, not folders



  • It can recurse down through subfolders (ok, so this one is not a limitation)



For more info on it, please see:

  • How to Use xp_delete_file to Purge Old Backup Files



  • SQL Server xp_delete_file not deleting files



The newer sys.xp_delete_files has the following syntax:
EXEC sys.xp_delete_files 'fileSpec.01' [, 'fileSpec.02' [, ...] ] ;


Notes:

  • File type / extension does not matter



  • Works with standard DOS wildcard characters:



  • * = zero or more of any character



  • ? = exactly one of any character



  • Does NOT recurse through subdirectories (for deleting files) (this is the only "limitation")



  • Can specify multiple, fully-qualified path specifications, each being able to handle wildcards



  • Can remove entire non-empty subfolder structure!! (might need to run a few times to delete everything)



  • Must be a member of the sysadmin fixed Server Role in order to execute (use Module Signing instead of adding application Login to sysadmin fixed Server Role; please see: Safely and Easily Use High-Level Permissions Without Granting Them to Anyone: Server-level )



  • Backported to SQL Server 2017 (possibly in CU18)



For complete details, please see my post:

sys.xp_delete_files and ‘allow filesystem enumeration’: two new undocumented items in SQL Server 2019

Context

StackExchange Database Administrators Q#258479, answer score: 13

Revisions (0)

No revisions yet.