Drive Cleanup Script

  • So I have some stored procedures that clean up a specific backup folder every night to keep a certain number of day's backups on hand. How do I create a script to delete old .bak files older than 90 days, across the entire drive?

  • Is this a local drive or a networked backup location? Seems risky to want to wipe out filetypes across and entire drive...

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • Local drive. Just want to specify (for example) D:\ and have it search the entire drive for old .bak flies.

  • I actually create a job using the maintenance plan feature of SQL Server to do this. I create the plan that deletes all differential backups. I then go and add a step before this step to do a full backup. So after my fill backup, it deletes all of my diff's. Acceptable for our company's risk tolerance and space usage, but may not be for your company. However, the maintenance plan created will allow you to choose how long you want to keep these files.

    Jared
    CE - Microsoft

  • That's not really what I am looking for but thanks. We enforce disk quotas for the developers so we don't want a ton of old crap laying around in various folders. Simply doing a recursive delete on the D: drive for .bak files older than 90 days is all I need.

  • Again, using a maintenance plan and using the cleanup task will allow you to specify the extension for files to delete (bak), an age (>90 days), a file path and an option to include all sub-folders, and finally a schedule. I think this is exactly what you are looking for.

    Jared
    CE - Microsoft

  • I agree with Jared, sounds like the way to go

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • I will give that a shot and post back if I get stuck.

  • ahthomas (2/13/2012)


    That's not really what I am looking for but thanks. We enforce disk quotas for the developers so we don't want a ton of old crap laying around in various folders. Simply doing a recursive delete on the D: drive for .bak files older than 90 days is all I need.

    Are you deleting from the backup history in the log files in MSDB? If you're not, you're only doing a part of the job that a well laid out maintenance plan would take care of automatically.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Forgot to follow up on this yesterday. Looks like the maintenance plan goes only one subdirectory level down from D:\, so this doesn't work for what I need it to do. I need a FULL scan of the HDD for .bak files older than 90 days.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply