Removing log information from log file by using DBCC SHRINK FILE

  • Hi All,

    Can we remove log information from log file using DBCC SHRINK FILE( 'Logicalname', EmptyFile). Can any body please advise !!!!

  • EmptyFile is only valid when shrinking a data file. It's completely ignored when specified on a log file. Nothing can ever move log records around within a log file.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Well, but you can specify a new empty log file. In the past we did it with sp_attach_single_file_db, in this case a new log file is generated. Now you have to use CREATE DATABASE database_name FOR ATTACH instead as indicated in the article for sp_attach_single_file_db

    CREATE DATABASE database_name FOR ATTACH has an option to rebuild the log. The ATTACH_REBUILD_LOG automatically creates a new, 1-MB log file as CREATE DATABASE article says.

    I used detaching and attaching a database to replace an old big log with a new empty one.

    Yelena

    Regards,Yelena Varsha

  • You can, but bear in mind that only works if the database was shut down cleanly before removing the log. Otherwise you'll end up with a database that fails to attach and that you'll need to restore from backup or hack back in to the server and attempt to repair. It also breaks the log chain.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Gail,

    Yes, of course. There is a few conditions including that the DB has to be read-write. Also CREATE DATABASE article says

    FOR ATTACH_REBUILD_LOG requires the following:

    A clean shutdown of the database.

    All data files (MDF and NDF) must be available.

    Important:

    This operation breaks the log backup chain. We recommend that a full database backup be performed after the operation is completed. For more information, see BACKUP (Transact-SQL).

    One needs to read on the topic and test in Dev first.

    Yelena

    Regards,Yelena Varsha

  • If what you want to do is simply shrink an oversized log file as much as possible, here's what I do:

    1) Run a full databases backup

    2) Run a transaction log backup (if the DB is Full Recovery Model)

    3) Run DBCC SHRINKFILE('logical_log_file_name',1)

    This will remove the data from the log file that is no longer needed.

    Dan

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply