I use Ola Hallengren's maintenance for all of my customer's, https://ola.hallengren.com/sql-server-backup.html
Have used it for years, never any real problems -- until now. For some reason the @CleanupMode and @CleanupTime are not working. The backup step is configured to retain bak files for only 24hrs, and to cleanup the old bak before generating the new. For some reason, it is not working, and the job keeps failing due to insufficient disk space.
My temporary workaround is below. I am deleting the largest bak file from disk as a first step in the job, until I get Ola's logic corrected. Well, it works great as-is, but I've found I need to run it for all subdirectories.
The way it works now is to del the bak file in this path explicitly:
SET @path = 'dir \\backups\servername\dbnam
I need it to delete the bak files from all subdirectories within \\backups\servername\
SET NOCOUNT ON;
IF OBJECT_ID('tempdb..#files') IS NOT NULL
DROP TABLE #files;
CREATE TABLE #files (Line VARCHAR(512))
DECLARE @path VARCHAR(49)
DECLARE @cmd1 VARCHAR(155)
DECLARE @cmd2 VARCHAR(155)
DECLARE @filename VARCHAR(255)
SET @path = 'dir \\backups\servername\dbname\FULL\'
SET @cmd1 = @Path+' /A-D /B'
EXEC master..xp_cmdshell @cmd1
DELETE #files WHERE Line IS NULL
--SELECT * FROM #files
SET @filename = (SELECT Line FROM #files)
SET @cmd2 = 'del \\backups\servername\dbname\FULL\' + @FileName
IF EXISTS(SELECT 1 FROM #files WHERE Line = @filename)
-- SELECT @cmd2
EXEC master..xp_cmdshell @cmd2
SET NOCOUNT OFF;