• Yeah...a few problems there. It probably starts having problems at this point:
    SET @path = @folder+'\'+QUOTENAME(@DBName)+'\'
     +RIGHT('0000'+CONVERT(nvarchar,DATEPART(yyyy,@now)),4)
     +RIGHT('00'+CONVERT(nvarchar,DATEPART(ww,@now)),2)

    You haven't set your variables @folder, @Now, @DBName to anything so you have some nulls when you are concatenating, so @path is null. Resulting in passing NULL to xp_delete_file.
    You also have an infinite loop more than a cursor 🙂  Remember you are doing that ugly row by row processing in a cursor so it's just doing fetches of each row. But your not fetching any next rows. So at the end of the the cursor block, you need to add the fetch before the END  - something like:
    FETCH NEXT FROM db_cursor INTO @DBName
    END

    And then you are building the path outside of the cursor and using @DBName so that won't work as you are looping through the databases to get the name in the cursor so that is where you would need to build the path if that's part of the path to the backup files you want to delete.
    It's pretty easy to do these in Powershell. A quick, rough example to give you an idea - if I want to delete any full backups in the path for all of the backups (so it applies when you do have the backups for each database in it's own folder)  and delete any that are more than 30 days old, I can just do something like:
    $BackupLocation= "X:\SQLServer\Backup"
    $FileExt = ".bak"
    $DaysToKeep = 30
    Get-ChildItem -Path $BackupLocation -Recurse|
     where-object {$_.CreationTime -lt (Get-Date).AddDays(-$DaysToKeep) -and $_.Extension -eq $FileExt} |
     Remove-Item

    Sue