question

Rob 2 avatar image
Rob 2 asked

xp_delete_file doesn't work...

I want to delete files from backup activities using xp_delete_files, I created a new maintenance plan to delete *.bak and *.trn files.

Both doesn't work and no error message is generated.

The T-SQL statement being made is:
EXECUTE master.dbo.xp_delete_file 0,N'D:\Backup\iWMC',N'bak',N'2010-05-11T12:48:38'

When I run it in a query window using my own account it fails as well without a message.

I checked
- that the files to be deleted are a result of an backup (the first 0)
- the path is correct no trailing \ (i tried a trailing \ and that doesn't work either)
- file extension doesn't has a . in it (i tried a leading . and that doesn't work either)
- the date contains a T and is of nchar type

The instance is version 9.0.3080 SP2 standard edition on a 64 bit platform.

backupadministrationdatafiles
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Rob 2 avatar image Rob 2 commented ·
When I try to delete 1 specific file I get: Executed as user: admin-vos. xp_delete_file() returned error 5, 'Access is denied.' [SQLSTATE 42000] (Error 22049). But I can delete the file using the explorer.
0 Likes 0 ·
Kev Riley avatar image
Kev Riley answered

You need to have sysadmin privileges - can you confirm that you are running the maintenance plan with a user with those rights, and that when you try it in a query window you have the right privileges.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Rob 2 avatar image Rob 2 commented ·
Thanks for your responce but I checked it and I have sysadmin. I used: SELECT SSP.name AS [Login Name], SSP.type_desc AS [Login Type], UPPER(SSPS.name) AS [Server Role] FROM sys.server_principals SSP INNER JOIN sys.server_role_members SSRM ON SSP.principal_id=SSRM.member_principal_id INNER JOIN sys.server_principals SSPS ON SSRM.role_principal_id = SSPS.principal_id; to check my server role, and is is sysadmin. Sorry for the mess but I can''t change it.
0 Likes 0 ·
ThomasRushton avatar image
ThomasRushton answered

The joys of using undocumented commands.

https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=124708&wa=wsignin1.0

However, there is some useful information here: http://sqlblog.com/blogs/andy_leonard/archive/2009/03/11/xp-delete-file.aspx - this link shows a further parameter that your code doesn't have. Perhaps this is the problem?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.