SQL Backup can apply different retention settings to either:
- backup files found on local and network shares (ERASEFILES and ERASEFILES_REMOTE) OR
- the backup and copyto locations (ERASEFILES_PRIMARY and ERASEFILES_SECONDARY)
What you could do is set up the backup task to use the ERASEFILES option to delete old files in the primary backup folder. You can't use ERASEFILES_REMOTE nor ERASEFILES_SECONDARY as it will apply to both the 6-month and 4-year backup folders. Instead, use the standalone file deletion procedure to clean up files in those 2 folders. You would need to set up a daily SQL Agent job that runs something like this:
EXEC master..sqlbackup '-sql "ERASE FULL_BACKUPS FOR [mydb] FROM DISK = [folder that holds 6 months worth of backups] KEEP = 190"'
EXEC master..sqlbackup '-sql "ERASE FULL_BACKUPS FOR [mydb] FROM DISK = [folder that holds 4 years worth of backups] KEEP = 1825"'
Another suggestion is if you back up a lot of databases, I would suggest you place the backup files in their own database-specific folder using the DATABASE tag i.e. instead of '... TO DISK = ''L:\Backup\<AUTO>.sqb'' ', use '... TO DISK = ''L:\Backup\<DATABASE>\<AUTO>.sqb''. This avoids SQL Backup having to scan all the files in a folder that holds backups from many different databases during the deletion process, thus saving disk and processing cycles and speeding up things considerably.
- backup files found on local and network shares (ERASEFILES and ERASEFILES_REMOTE) OR
- the backup and copyto locations (ERASEFILES_PRIMARY and ERASEFILES_SECONDARY)
What you could do is set up the backup task to use the ERASEFILES option to delete old files in the primary backup folder. You can't use ERASEFILES_REMOTE nor ERASEFILES_SECONDARY as it will apply to both the 6-month and 4-year backup folders. Instead, use the standalone file deletion procedure to clean up files in those 2 folders. You would need to set up a daily SQL Agent job that runs something like this:
EXEC master..sqlbackup '-sql "ERASE FULL_BACKUPS FOR [mydb] FROM DISK = [folder that holds 6 months worth of backups] KEEP = 190"'
EXEC master..sqlbackup '-sql "ERASE FULL_BACKUPS FOR [mydb] FROM DISK = [folder that holds 4 years worth of backups] KEEP = 1825"'
Another suggestion is if you back up a lot of databases, I would suggest you place the backup files in their own database-specific folder using the DATABASE tag i.e. instead of '... TO DISK = ''L:\Backup\<AUTO>.sqb'' ', use '... TO DISK = ''L:\Backup\<DATABASE>\<AUTO>.sqb''. This avoids SQL Backup having to scan all the files in a folder that holds backups from many different databases during the deletion process, thus saving disk and processing cycles and speeding up things considerably.