Remove unwanted multiple ldf files in sql server

Shashikala Bhat 1 Reputation point
2020-08-17T13:12:54.827+00:00

Hi,

I have to create a procedure to remove unwanted ldf files in sql servers.

DBCC loginfo('DBName') provides the VLF status.
Based on this i need to select the log file name which are not in use and create a procedure to remove ldf file.

Thanks
Shashikala

SQL Server Other
{count} votes

3 answers

Sort by: Most helpful
  1. Tom Phillips 17,771 Reputation points
    2020-08-17T18:59:02.763+00:00
    0 comments No comments

  2. CathyJi-MSFT 22,396 Reputation points Microsoft External Staff
    2020-08-18T02:46:41.597+00:00

    Hi Shashikala,

    We can use Alter database statement and add the REMOVE FILE clause and specify the file that we want to remove. For example:

    ALTER DATABASE [MultipleLogFiles] REMOVE FILE [MultipleLogFiles_log_1]
    GO

    Please refer to Drop unwanted (secondary) SQL Server transaction log files to get more detail steps.

    If the response helped, do "Accept Answer" and up vote it.

    Best regards.
    Cathy


  3. tibor_karaszi@hotmail.com 4,316 Reputation points
    2020-08-18T07:27:37.627+00:00

    Assuming that you are on SQL server 2017 or higher, you can use the new dmv instead of DBCC LOGINFO. You can query that to find out which file(s) has no used VLFs. Based on that (using a cursor to loop the result from the query) you can generate the command to delete the file and execute that command using dynamic SQL. Not particular difficult T-SQL programming. Here's the query that your cursor will use, to get you started:

    SELECT 
     file_id
    ,SUM(CAST(vlf_active AS tinyint)) AS status_of_file
    FROM sys.dm_db_log_info(DB_ID()) 
    GROUP BY file_id
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.