Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 118445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:29:45+00:00 2026-05-11T03:29:45+00:00

I’m trying to write some SQL that will delete files of type ‘.7z’ that

  • 0

I’m trying to write some SQL that will delete files of type ‘.7z’ that are older than 7 days.

Here’s what I’ve got that’s not working:

DECLARE @DateString CHAR(8) SET @DateString = CONVERT(CHAR(8), DATEADD(d, -7, GETDATE()), 1) EXECUTE master.dbo.xp_delete_file 0,                    N'e:\Database Backups',N'7z', @DateString, 1 

I’ve also tried changing the ‘1’ at the end to a ‘0’.

This returns ‘success’, but the files aren’t getting deleted.

I’m using SQL Server 2005, Standard, w/SP2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:29:45+00:00Added an answer on May 11, 2026 at 3:29 am

    Had a similar problem, found various answers. Here’s what I found.

    You can’t delete 7z files with xp_delete_file. This is an undocumented extended stored procedure that’s a holdover from SQL 2000. It checks the first line of the file to be deleted to verify that it is either a SQL backup file or a SQL report file. It doesn’t check based on the file extension. From what I gather its intended use is in maintenance plans to cleanup old backups and plan reports.

    Here’s a sample based on Tomalak’s link to delete backup files older than 7 days. What trips people up is the ‘sys’ schema, the trailing slash in the folder path, and no dot in the file extension to look for. The user that SQL Server runs as also needs to have delete permissions on the folder.

    DECLARE @DeleteDate datetime SET @DeleteDate = DateAdd(day, -7, GetDate())  EXECUTE master.sys.xp_delete_file 0, -- FileTypeSelected (0 = FileBackup, 1 = FileReport) N'D:\SQLbackups\', -- folder path (trailing slash) N'bak', -- file extension which needs to be deleted (no dot) @DeleteDate, -- date prior which to delete 1 -- subfolder flag (1 = include files in first subfolder level, 0 = not) 

    Note that xp_delete_file is broken in SP2 and won’t work on report files; there’s a hotfix for it at [http://support.microsoft.com/kb/938085%5D. I have not tested it with SP3.

    Since it’s undocumented, xp_delete_file may go away or change in future versions of SQL Server. Many sites recommend a shell script to do the deletions instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.