Manually Purge Log Files from Exchange Database Copies

Purpose:  The purpose of this document is to provide instruction on how to safely purge Exchange Database log files manually after log file buildup on a Exchange Server.  Log files are stored in the logs directly until an Exchange aware Full or Incremental Backup is successfully ran.  Once Exchange is aware that this backup is complete, Exchange will purge the log files.  If a backup has not ran successfully, then log files will continue to be created in the logs directory and the previous log files will still exist, consuming space.  Over time, this will fill up the hard drive where the log files are kept.  This manual purge is done live, meaning the instructions below happen while the Exchange Database remains mounted.  In the instructions below, we are using an Exchange Database DB53 that has 4 Database Copies; 1 Mounted, 3 Healthy.


Overview of the Instructions Below:

1. Capture and isolate the Log Files that will moved and purged into a text file.

2. Using Excel, open that text file and format it. Format meaning remove unwanted rows such as Header/Footer rows and removing log files for dates you wish to not remove.  For Example, I want to remove all the log files EXCEPT the last day or two. **NOTE do not remove the newest log files – give yourself a buffer**.

3. Using Excel, we will formulate the Move-Item and Remove-Item scripts to be ran on each server that holds the database copy.  From one database copy, we will move the log files to another server\storage location.  From the other 3 database copies, we will purge the log files.  Purging log files is quick, moving log files is a little bit slower.

4. Run the appropriate .ps1 script that will be created on each server that holds a database copy.


Instructions:

Capture and Isolate the Log Files:
1. Determine the LogFilePrefix for the database:
get-mailboxdatabase DB53 | FL LogFilePrefix

2. Determine the Database Copies Status to figure out which copy is mounted, healthy, or other status. Of the Mounted/Healthy Database Copies, choose 1 Database Copy to move the logs that wish to be purged to a storage share and purge the logs on the remaining copies. This ensures a copy of the logs exist in the event of a problem.

Get-mailboxdatabasecopystatus DB53

3. In a command prompt, run the following DIR command. It will put the results into the text file specified.  You will be using the LogFilePrefix you have found in Step1.

dir \\MBX01\e$\db53\logs\E01*.log > C:\users\steve\desktop\DB53.txt



In Excel, Open the Text File and Format It

4. Open Excel and then Open that Txt File.  Make sure during the Text Import Wizard (when opening the txt file in Excel), the logs are in their own column. I chose fixed width in the Wizard. Click on Finish.

5. Remove the rows that do not contain a log file (Very Beginning Rows and Very Last Rows).

6. Sort the dates (if not sorted already) and remove the rows of the date range that you do not wish to delete. For Example, I want to purge all log files EXCEPT for the last 2 days’ worth.


In Excel, Create the PowerShell Code used to Move/Purge Log Files

7. In Column F (Or the next Open Column) type this formula in and Copy on down to the last row. Note: Cell E2 is the log file name. You may have to change this value if the log file name exists in a different cell.

="Write-Host "&E2&"; Move-Item E:\DB53\Logs\"&E2&" \\FileSvr1\files\DB53" 

8. In Column G (or the next Open Column) type this formula in and Copy on down to the last row. Note: Cell E2 is the log file name. You may have to change this value if the log file name exists in a different cell.

="Write-Host "&E2&"; Remove-Item E:\DB53\Logs\"&E2&" -force"

 9. Copy the Contents out of the Move-Item Column and paste into notepad. Save that notepad file as DB53_move.ps1.  Copy this and run this on the server that has the database copy for you wish to move the log files.

10. Copy the Contents out of the Remove-Item Column and paste into notepad. Save that notepad file as DB53_remove.ps1.  Run this in PowerShell on the servers that have the remaining database copies.


Now you have carefully Moved/Purged the Log Files of that database. You now have bought yourself enough free space on the hard drives to work out your backup issues.