DBCC SHRINKFILE on log file not reducing size even after BACKUP LOG TO DISK

I’ve got a database, [My DB], that has the following info:
SQL Server 2008
MDF size: 30 GB
LDF size: 67 GB

I wanted to shrink the log file as much as possible and so I started my quest to figure out how to do this. Caveat: I am not a DBA (or even approaching a DBA) and have been progressing by feel through this quest.

  • Conditionally set a variable/parameter in an SSIS Expression
  • Case sensitive variables in SQL Server
  • SQL to join nvarchar(max) column with int column
  • SqlCommand read one value
  • How to analayze/display a raw web analytics data?
  • Rolling up multiple rows into single row using 2 tables
  • First, I just went into SSMS, DB properties, Files, and edited the Initial Size (MB) value to 10. That reduced the log file to 62 GB (not exactly the 10 MB that I entered). So, I attached SQL Profiler, saw that DBCC SHRINKFILE was being called. I then entered that command into the query editor and here’s the results.

    DBCC SHRINKFILE (N'My DB_Log' , 10)
    

    And the output was:

    Cannot shrink log file 2 (My DB_Log) because the logical log file located at the end of the file is in use.
    DbId   FileId      CurrentSize MinimumSize UsedPages   EstimatedPages
    ------ ----------- ----------- ----------- ----------- --------------
    8      2           8044104     12800       8044104     12800
    
    (1 row(s) affected)
    
    DBCC execution completed. If DBCC printed error messages, contact your system administrator.
    

    I then did some research on that and found this:

    http://support.microsoft.com/kb/907511

    Which says that I need to backup the log file before the shrinkfile so that the virtual log files will be released and the shrinkfile can do its job – I don’t know what that means… I’m just paraphrasing here 🙂

    So, I figured I’d try to backup the log file and then do a DBCC SHRINKFILE (and I changed the new log file size to 12800 since that was the MinimumSize identified in the output of the previous DBCC SHRINKFILE command)

    BACKUP LOG [My DB] TO DISK = 'D:\SQLBackup\20110824-MyDB-Log.bak'
    GO
    DBCC SHRINKFILE (N'My DB_Log' , 12800)
    GO
    

    The result was the same as the first go around. I can only get the log file down to 62 GB.

    I’m not sure what I’m doing wrong and what I should try next.

    8 Solutions collect form web for “DBCC SHRINKFILE on log file not reducing size even after BACKUP LOG TO DISK”

    In addition to the steps you have already taken, you will need to set the recovery mode to simple before you can shrink the log.

    THIS IS NOT A RECOMMENDED PRACTICE for production systems… You will lose your ability to recover to a point in time from previous backups/log files.

    See example B on this DBCC SHRINKFILE (Transact-SQL) msdn page for an example, and explanation.

    Okay, here is a solution to reduce the physical size of the transaction file, but without changing the recovery mode to simple.

    Within your database, locate the file_id of the log file using the following query.

    SELECT * FROM sys.database_files;
    

    In my instance, the log file is file_id 2. Now we want to locate the virtual logs in use, and do this with the following command.

    DBCC LOGINFO;
    

    Here you can see if any virtual logs are in use by seeing if the status is 2 (in use), or 0 (free). When shrinking files, empty virtual logs are physically removed starting at the end of the file until it hits the first used status. This is why shrinking a transaction log file sometimes shrinks it part way, but does remove all free virtual logs which you may expect.

    If you notice a status 2’s that occur after 0’s, this is blocking the shrink from fully shrinking the file. To get around this do another transaction log backup, and immediately run these commands, supplying the file_id found above, and the size you would like your log file to be reduced to.

    DBCC SHRINKFILE (file_id, LogSize_MB)

    DBCC SHRINKFILE (2, 100);
    DBCC LOGINFO;
    

    This will then show the virtual log file allocation, and hopefully you’ll notice that it’s been reduced somewhat. Because virtual log files are not always allocated in order, you may have to backup the transaction log a couple of times and run this last query again; but I can normally shrink it down within a backup or two.

    Full article: http://radderz.me.uk/2014/02/physically-shrinking-sql-transaction-log-files/

    I use this script on sql server 2008 R2.

    USE [db_name]
    
    ALTER DATABASE [db_name] SET RECOVERY SIMPLE WITH NO_WAIT
    
    DBCC SHRINKFILE([log_file_name]/log_file_number, wanted_size)
    
    ALTER DATABASE [db_name] SET RECOVERY FULL WITH NO_WAIT
    

    Try this

    ALTER DATABASE XXXX  SET RECOVERY SIMPLE
    
    use XXXX
    
    declare @log_File_Name varchar(200) 
    
    select @log_File_Name  = name from sysfiles where filename like '%LDF'
    
    declare @i int = FILE_IDEX ( @log_File_Name)
    
    dbcc shrinkfile ( @i , 50) 
    

    Shrinking a Log File

    For log files, the Database Engine uses target_size to calculate the target size for the whole log; therefore, target_size is the amount of free space in the log after the shrink operation. Target size for the whole log is then translated to target size for each log file. DBCC SHRINKFILE tries to shrink each physical log file to its target size immediately.

    However, if part of the logical log resides in the virtual logs beyond the target size, the Database Engine frees as much space as possible, and then issues an informational message.

    The message describes what actions are required to move the logical log out of the virtual logs at the end of the file. After the actions are performed, DBCC SHRINKFILE can be used to free the remaining space.

    Because a log file can only be shrunk to a virtual log file boundary, shrinking a log file to a size smaller than the size of a virtual log file might not be possible, even if it is not being used. The size of the virtual log file is chosen dynamically by the Database Engine when log files are created or extended.

    • Troubleshooting: The File Does Not Shrink

    If the shrink operation runs without error, but the file does not appear to have changed in size, verify that the file has adequate free space to remove by performing one of the following operations:

    Run the following query.

    SELECT name ,size/128.0 – CAST(FILEPROPERTY(name, ‘SpaceUsed’) AS
    int)/128.0 AS AvailableSpaceInMB FROM sys.database_files;

    Run the DBCC SQLPERF command to return the space used in the transaction log.

    • If insufficient free space is available, the shrink operation cannot reduce the file size any further.

    • Typically it is the log file that appears not to shrink. This is usually the result of a log file that has not been truncated.

    • You can truncate the log by setting the database recovery model to SIMPLE, or by backing up the log and then running the DBCC SHRINKFILE operation again.

    Example :

    Shrinking a log file to a specified target size

    The following example shrinks the log file in the AdventureWorks database to 1 MB. To allow the DBCC SHRINKFILE command to shrink the file, the file is first truncated by setting the database recovery model to SIMPLE.

    Transact-SQL

    USE AdventureWorks2012;
    GO
    — Truncate the log by changing the database recovery model to SIMPLE.
    ALTER DATABASE AdventureWorks2012
    SET RECOVERY SIMPLE;
    GO
    — Shrink the truncated log file to 1 MB.
    DBCC SHRINKFILE (AdventureWorks2012_Log, 1);
    GO
    — Reset the database recovery model.
    ALTER DATABASE AdventureWorks2012
    SET RECOVERY FULL;
    GO

    When you use DBCC SHRINKFILE(Logfile, size) it only truncates from the end of the log file back as far as it can go. When it reaches the highest virtual log still in use, it cannot shrink further. This is described in the SQL Server Books Online at:

    http://technet.microsoft.com/en-us/library/ms189493.aspx

    So, once the high end of the log is clear, it can be shrunk down in size. Again, that will depend on how much of the log is still in use. The log can be cleared by backups, but the backups will not clear incomplete transactions, so the log can remain in a high-end VLF even after repeated backups.

    With regard to the increase and decrease of VLFs, how big was the log file created to be originally and what is the setting for log file growth? If it grows by only a small amount it will create more VLFs than anyone desires.

    A common pattern for shrinking a log file is CHECKPOINT, BACKUP, SHRINKFILE, CHECKPOINT, BACKUP, SHRINKFILE, etc until you get results. There are many reasons that the log may not be shrinkable, including a very large rollback.

    Switching from Simple to Full has a Problem:

    There are rules and exceptions here. We’ll talk about long running transactions in depth below.

    But one caveat to keep in mind for Full Recovery Mode is this: If you just switch into Full Recovery mode, but never take an initial Full Backup, SQL Server will not honor your request to be in Full Recovery model. Your transaction log will continue to operate as it has in Simpleuntil you switch to Full Recovery Model AND take your first Full Backup.

    Full Recovery Model without log backups is bad:

    So, that’s the most common reason for uncontrolled log growth? Answer: Being in Full Recovery mode without having any log backups.

    This happens all the time to people.

    Why is this such a common mistake?

    Why does it happen all the time? Because each new database gets its initial recovery model setting by looking at the model database.

    Model’s initial recovery model setting is always Full Recovery Model – until and unless someone changes that. So you could say the “default Recovery Model” is Full. Many people are not aware of this and have their databases running in Full Recovery Model with no log backups, and therefore a transaction log file much larger than necessary. This is why it is important to change defaults when they don’t work for your organization and its needs)

    I tried many ways but this works.

    Sample code is availalbe in DBCC SHRINKFILE

    USE DBName;  
    GO  
    -- Truncate the log by changing the database recovery model to SIMPLE.  
    ALTER DATABASE DBName  
    SET RECOVERY SIMPLE;  
    GO  
    -- Shrink the truncated log file to 1 MB.  
    DBCC SHRINKFILE (DBName_log, 1);  --File name SELECT * FROM sys.database_files; query to get the file name
    GO  
    -- Reset the database recovery model.  
    ALTER DATABASE DBName  
    SET RECOVERY FULL;  
    GO
    

    Thanks to @user2630576 and @Ed.S.

    the following worked a treat:

    BACKUP LOG [database] TO DISK = 'D:\database.bak'
    GO
    
    ALTER DATABASE [database] SET RECOVERY SIMPLE
    
    use [database]
    
    declare @log_File_Name varchar(200)
    
    select @log_File_Name = name from sysfiles where filename like '%LDF'
    
    declare @i int = FILE_IDEX ( @log_File_Name)
    
    dbcc shrinkfile ( @i , 50)
    
    ALTER DATABASE [database] SET RECOVERY FULL
    
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.