Introduction
This article explains how to automatically delete “old .log files” from MT5 (MetaTrader 5) “operation logs (Logs/Journal),” “expert logs (MQL5\Logs/Experts),” and “backtest logs (Tester\logs)” to prevent excessive disk space usage. This can be achieved using standard Windows Batch (.bat) or PowerShell (.ps1). This guide is for those who want to automate the process described in the related article (MT5 Log Bloat Countermeasures | How to Delete Operation, Expert, and Backtest Logs to Reduce Size).
Automatic Deletion Concept (Prerequisites)
- MT5 logs are saved in
YYYYMMDD.logformat, one for each date. - The basic deletion condition is “files older than $\text{X}$ days” (e.g., older than 30 days).
- The target folders are primarily three:
Logs,MQL5\Logs, andTester\logs. - By registering it with the Windows “Task Scheduler,” you can automate daily/weekly execution.
Confirming Target Folders and Paths
Open Explorer from the MT5 top menu “File → Open Data Folder” and check the following subfolders:
…\Terminal\<Long ID>\Logs(Operation Logs)…\Terminal\<Long ID>\MQL5\Logs(Expert Advisors/Indicators)…\Terminal\<Long ID>\Tester\logs(Backtest Logs)
If you use multiple terminals (multiple IDs), there are log folders with the same name under each ID. For the portable version of MT5, MQL5 and Tester are located directly under the main folder.
Retention Period (Operational Policy Guideline)
- Live Operation (including VPS): Keep 14–30 days
- Development/Testing: 7–14 days (Shorter retention recommended due to high log volume)
- Backtest Only: Delete weekly or daily, according to the test cycle
Start with a longer period, such as 30 days, and gradually reduce it if there are no issues.
Method A: Automatic Deletion with Batch File (.bat)
A-1 Recommended: Bulk Toggle with DRYRUN Flag (Supports Multiple Terminals)
This configuration allows for bulk switching between “list only” and “production deletion” using the DRYRUN flag to prevent accidental deletion. It automatically scans all IDs (terminals) under %APPDATA%\MetaQuotes\Terminal.
This is convenient when running multiple MT5 instances on a single PC or VPS.
@echo off
REM ===== MT5 Log Auto-Deletion (Batch Version / Multiple Terminals & Bulk Toggle) =====
REM Running as administrator is recommended. Change DAYS/DRYRUN as needed.
setlocal enabledelayedexpansion
set "DAYS=30" REM Target files older than this many days (e.g., 30)
set "DRYRUN=1" REM 1=List only (no deletion), 0=Production deletion
set "DATA_ROOT=%APPDATA%\MetaQuotes\Terminal"
echo [INFO] MT5 logs cleanup target: files older than %DAYS% days
echo [MODE] DRYRUN=%DRYRUN% (1=list only, 0=delete)
echo [ROOT] %DATA_ROOT%
echo.
for /D %%T in ("%DATA_ROOT%\*") do (
for %%F in ("%%T\Logs" "%%T\MQL5\Logs" "%%T\Tester\logs") do (
if exist "%%~F" (
echo [CLEAN] %%~F
if "!DRYRUN!"=="1" (
REM List only (no deletion)
forfiles /p "%%~F" /m *.log /d -%DAYS% /c "cmd /c echo @path"
) else (
REM Production deletion (silent deletion / error suppression)
forfiles /p "%%~F" /m *.log /d -%DAYS% /c "cmd /c del /f /q @path" 2>nul
)
)
)
)
echo.
echo [DONE] Completed log cleanup.
endlocal
How to Toggle: For testing, use DRYRUN=1 (list only). To execute deletion, change DRYRUN=0 and save the file. You don’t need to comment/uncomment lines.
A-2 Specify with Fixed ID Path (Single Terminal)
This is a simplified version targeting only a specific terminal ID (single environment). Replace xxxxxxx with your ID.
@echo off
REM ===== MT5 Log Auto-Deletion (Batch Version / Fixed ID & Bulk Toggle) =====
setlocal enabledelayedexpansion
set "DAYS=30"
set "DRYRUN=1" REM 1=List only, 0=Delete
REM Specify log folders in the MT5 data folder (replace ID)
set "LOGS_PATH=C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\Logs"
set "EXPERTS_LOGS=C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\MQL5\Logs"
set "TESTER_LOGS=C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\Tester\logs"
echo [INFO] DAYS=%DAYS% / DRYRUN=%DRYRUN%
for %%F in ("%LOGS_PATH%" "%EXPERTS_LOGS%" "%TESTER_LOGS%") do (
if exist "%%~F" (
echo [CLEAN] %%~F
if "%DRYRUN%"=="1" (
forfiles /p "%%~F" /s /m *.log /d -%DAYS% /c "cmd /c echo @path"
) else (
forfiles /p "%%~F" /s /m *.log /d -%DAYS% /c "cmd /c del /f /q @path" 2>nul
)
)
)
echo [DONE] Completed log cleanup.
endlocal
A-3 Minimal Configuration (No DRYRUN, Immediate Deletion)
A minimal example for immediate deletion (no testing). **Use caution to prevent accidental deletion.**
@echo off
REM Specify log folders in the MT5 data folder
set LOGS_PATH="C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\Logs"
set EXPERTS_LOGS="C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\MQL5\Logs"
set TESTER_LOGS="C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\Tester\logs"
REM Delete files older than 30 days
forfiles /p %LOGS_PATH% /s /m *.log /d -30 /c "cmd /c del @file"
forfiles /p %EXPERTS_LOGS% /s /m *.log /d -30 /c "cmd /c del @file"
forfiles /p %TESTER_LOGS% /s /m *.log /d -30 /c "cmd /c del @file"
Method B: Automatic Deletion with PowerShell (.ps1)
B-1 Recommended: $DryRun Flag for Bulk Toggle (Supports Multiple Terminals)
PowerShell allows safe dry runs using -WhatIf. You can manage the operation simply by toggling $DryRun between $true and $false.
# ===== MT5 Log Auto-Deletion (PowerShell Version / Multiple Terminals & Bulk Toggle) =====
# Running as administrator is recommended. Adjust $Days / $DryRun as needed.
$Days = 30
$DryRun = $true # $true=List only (no deletion), $false=Production deletion
$cutoff = (Get-Date).AddDays(-$Days)
$dataRoot = Join-Path $env:APPDATA 'MetaQuotes\Terminal'
Write-Host "[INFO] Delete logs older than $Days days"
Write-Host "[MODE] DryRun=$DryRun"
Write-Host "[ROOT] $dataRoot`n"
$terminals = Get-ChildItem -Path $dataRoot -Directory -ErrorAction SilentlyContinue
foreach ($t in $terminals) {
$folders = @(
Join-Path $t.FullName 'Logs'
Join-Path $t.FullName 'MQL5\Logs'
Join-Path $t.FullName 'Tester\logs'
)
foreach ($folder in $folders) {
if (Test-Path $folder) {
Write-Host "[CLEAN] $folder"
$targets = Get-ChildItem -LiteralPath $folder -Filter *.log -File -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -lt $cutoff }
if ($DryRun) {
$targets | ForEach-Object { Write-Host $_.FullName }
} else {
$targets | Remove-Item -Force -ErrorAction SilentlyContinue
}
}
}
}
Write-Host "`n[DONE] Completed log cleanup."
B-2 Specify with Fixed ID Path (Single Terminal)
This version targets a specific ID only. Replace xxxxxxx with your ID.
# ===== MT5 Log Auto-Deletion (PowerShell Version / Fixed ID & Bulk Toggle) =====
$Days = 30
$DryRun = $true
$limit = (Get-Date).AddDays(-$Days)
$folders = @(
"C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\Logs",
"C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\MQL5\Logs",
"C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\Tester\logs"
)
Write-Host "[INFO] DAYS=$Days / DryRun=$DryRun"
foreach ($folder in $folders) {
if (Test-Path $folder) {
Write-Host "[CLEAN] $folder"
$targets = Get-ChildItem -Path $folder -Filter *.log -File -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -lt $limit }
if ($DryRun) {
$targets | ForEach-Object { Write-Host $_.FullName }
} else {
$targets | Remove-Item -Force -ErrorAction SilentlyContinue
}
}
}
Write-Host "[DONE] Completed log cleanup."
B-3 Minimal Configuration (Immediate Deletion)
A minimal example for immediate deletion (no testing). **Use caution to prevent accidental deletion.**
$days = 30
$limit = (Get-Date).AddDays(-$days)
$folders = @(
"C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\Logs",
"C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\MQL5\Logs",
"C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\xxxxxxx\Tester\logs"
)
foreach ($folder in $folders) {
Get-ChildItem -Path $folder -Filter *.log | Where-Object { $_.LastWriteTime -lt $limit } | Remove-Item -Force
}
Schedule Regular Execution with Task Scheduler
Steps
- Open Windows “Task Scheduler.”
- “Create Basic Task” → Enter a Name (e.g., MT5 Log Auto-Deletion).
- Select the Trigger (Daily/Weekly. A time with low load, such as late at night, is recommended).
- Action = “Start a program.”
- Batch Version: Program/script:
cmd.exe
Add arguments:/c "C:\scripts\DeleteMT5Logs.bat" - PowerShell Version: Program/script:
powershell.exe
Add arguments:-ExecutionPolicy Bypass -File "C:\scripts\DeleteMT5Logs.ps1"
- Batch Version: Program/script:
- If necessary, enable “Run whether user is logged on or not” and “Run with highest privileges” (recommended for VPS).
- Disable “Power” restrictions in Conditions (for VPS or always-on PCs).
- Enable “Task History” and confirm success/failure (
0x0is success).
Safe Operation Checklist
- Only target
.logfiles for deletion (do not deleteBases,History,Profiles, etc.). - Always run a dry run first:
DRYRUN=1for Batch,$DryRun=$truefor PowerShell. - Start with a longer retention period → shorten if no issues arise.
- When using multiple instances, ensure all ID subfolders are targeted (the script in this article handles this automatically).
- Be careful of insufficient permissions or incorrect paths (use paths copied directly from “Open Data Folder”).
Troubleshooting
- Blocked by Execution Policy: Start PowerShell with
-ExecutionPolicy Bypass. - Access Denied: Run with administrator privileges / Grant “highest privileges” to the task.
- Targets Not Found: Recheck the path for the portable version, subfolders for multiple IDs, and folder name spelling.
- Schedule Not Running: Check “Last Run Result” (
0x0is success), startup user/conditions settings, and history.
FAQ
- Q. Which folders and what files should I delete?
- A. Only
.logfiles inLogs,MQL5\Logs, andTester\logs. Do not deleteBases,History, orProfiles. - Q. What is a safe retention period?
- A. Start with about 30 days, and if there are no issues, you can shorten it to 14 or 7 days. 14–30 days is a guideline for live operation, and 7–14 days for development/testing.
- Q. I use multiple MT5 instances (multiple IDs). Can all of them be targeted?
- A. The samples in this article automatically scan all ID folders under
%APPDATA%\MetaQuotes\Terminal\. - Q. Does deletion affect EAs or backtest results?
- A. It only removes past output records (logs) and does not affect the EA logic or test results.
- Q. I want to check the files before deleting them.
- A. Set
DRYRUN=1for Batch or$DryRun=$truefor PowerShell, and run the script to display a list only. - Q. Can I use this for the portable version of MT5?
- A. Yes. Please specify the absolute path to the
Logs/MQL5\Logs/Tester\logsfolders located directly under the portable version’s main folder in the script.

