2

Is it possible to run Ola Hallengren's backup scripts in cleanup only mode? I have a couple of servers where the cleanup time was set too long, and I now have to clean up hundreds of databases worth of backups. I could bang something together with PowerShell or similar, but I'd be more comfortable with some known good code handling it instead.

1
  • 1
    Have you tried using the CleanupMode parameter and setting it to BEFORE_BACKUP?
    – nkdbajoe
    Commented Aug 18, 2020 at 23:01

1 Answer 1

0

There are a few ways to try this, but from what I can tell they either won't work, or will still have to do at least part of the backup process, or will require modifications to Ola's stored procedures.

Don't back up any databases?

Looking at the code inside Ola's DatabaseBackup stored procedure, the cleanup portion is inside the loop that goes through all of the databases that are to be backed up. So passing a dummy parameter or just asking it to back up only a small token database isn't going to delete old backup files from the other databases.

Backup to NUL?

You can pass NUL as the directory, but the code has a check for that and won't perform cleanup if you are writing the backups to NUL.

  IF @CleanupTime IS NOT NULL AND EXISTS(SELECT * FROM @Directories WHERE DirectoryPath = 'NUL')
    BEGIN
      INSERT INTO @Errors ([Message], Severity, [State])
        SELECT 'The value for the parameter @CleanupTime is not supported. Cleanup is not supported when backing up to NUL.', 16, 4
  END

And even if you commented that check out, it will try to clean up from the same place you are writing the current backups to. So that won't help.

CopyOnly backups?

Using the CopyOnly parameter will prevent these one-off backups from affecting your backup chain. But the directory and/or filename will have a CopyOnly indicator in there, so the only old files that will get deleted are only going to be other CopyOnly backups.

Modify the code?

You might be able to comment out the line that executes the "Backup Database..." command (something like EXECUTE @CurrentCommandOutput =...), and use the Before_Backup value for the CleanupMode parameter. That way, the cleanup will happen and then the backup will be skipped. However, there are a lot of checks in the code to prevent things from going sideways, and I haven't read through it in enough details to know if commenting out that line would be safe. I wouldn't recommend it.

Let it work as designed?

Can you just correct the CleanupTime parameter that was too long, and let it clean everything up the next time it runs? Is there a reason you need to do the cleanup before the next scheduled backup?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.