PM2 keeps application output in log files, which can grow over time and consume valuable disk space. If you are wondering how to clear PM2 logs, the quickest option is usually the built-in pm2 flush command. It empties PM2’s managed log files without stopping your applications. You can also clear logs for a particular app, truncate files manually, or configure automatic log rotation to prevent them from growing too large again.
This guide explains each method, where PM2 stores logs, and how to avoid common mistakes. Choose the approach that fits your setup, and make sure you are working as the same operating-system user that runs your PM2 processes.
The Quickest Way to Clear PM2 Logs
To empty all logs managed by the current PM2 instance, run:
pm2 flush
This clears the saved log contents for the applications managed by that PM2 daemon. It does not stop, restart, or delete your apps. New output will be written to the log files as soon as your applications produce it, so the files may begin growing again immediately.
Check the result by listing your processes and viewing recent output:
pm2 list
pm2 logs --lines 20
The first command shows your PM2-managed processes. The second displays recent log lines; it does not clear logs. If an app is quiet, its log may remain empty after flushing until it writes new output.
How to Clear Logs for One PM2 App
If you only want to clear one application’s logs, specify its PM2 name or process ID:
pm2 flush my-app
You can find the app name and ID with:
pm2 list
For example, if the list shows an application named api-server, use pm2 flush api-server. You can also use its PM2 ID, such as pm2 flush 2. Check that the name or ID matches the intended process before running the command, particularly on a production server.
Flushing a single app is useful when you are investigating one service and want to preserve the output from other apps. If your PM2 version does not accept a name or ID with flush, use the manual file-truncation method below for that app’s log files.
Where PM2 Stores Log Files
By default, PM2 stores logs in the logs directory under its home directory. On many Linux and macOS systems, the path is:
~/.pm2/logs/
Each application typically has separate files for standard output and errors. A process named my-app might have files such as my-app-out.log and my-app-error.log. The exact filenames can differ if you have configured custom output or error paths in your PM2 ecosystem file or startup command.
PM2’s home directory can be changed with the PM2_HOME environment variable. If you cannot find the logs in ~/.pm2/logs, check whether your deployment, service manager, container, or shell sets a custom PM2_HOME. Also confirm that you are using the same system account that started PM2. A user’s PM2 daemon and logs are generally separate from another user’s.
Manually Empty PM2 Log Files on Linux or macOS
When you need to clear files directly, truncating them is generally safer than deleting them. Truncation keeps the files in place but reduces their size to zero bytes. That matters because a running process or logging service may still have a file open.
To truncate all standard PM2 log files in the default directory, run:
find ~/.pm2/logs -type f -name '*.log' -exec truncate -s 0 {} \;
To clear only one app’s output and error logs, substitute its actual log filenames:
truncate -s 0 ~/.pm2/logs/my-app-out.log
truncate -s 0 ~/.pm2/logs/my-app-error.log
Some systems do not have the truncate utility installed. A shell alternative for an individual file is:
: > ~/.pm2/logs/my-app-out.log
Be careful with wildcards and paths. Before truncating a group of files, list them first so you can confirm what will be affected:
ls -lh ~/.pm2/logs/
If you see a permission error, do not assume that running the command with sudo is the right fix. First verify which user owns the PM2 daemon and files. Running PM2 commands as a different user can target a different PM2 instance and may create confusing ownership or service-management problems.
Clear PM2 Logs on Windows
PM2’s log directory on Windows depends on the account and configuration used to run PM2. Look under the PM2 home directory, commonly inside the user profile, and open its logs folder. If you set a custom PM2_HOME, use that location instead.
You can use PM2’s built-in command from PowerShell or Command Prompt:
pm2 flush
To empty a specific file in PowerShell, use its actual path:
Clear-Content "$env:USERPROFILE\.pm2\logs\my-app-out.log"
Close any editor or process that may be holding the file open if Windows reports that it cannot access it. For a large or important deployment, prefer testing the command with one known log file before clearing a whole directory.
Clear Logs Without Stopping PM2 Apps
In most cases, pm2 flush is the best answer to “How do I clear PM2 logs?” because it clears the logs while leaving PM2-managed processes running. You do not need to stop or delete an app just to free space in its log files.
Avoid using pm2 delete or pm2 kill as log-clearing commands. pm2 delete removes a process from PM2’s process list, while pm2 kill stops the PM2 daemon and its managed processes. Neither is necessary when your goal is simply to empty log files.
Manual deletion with rm is also less reliable than truncation when a process still has the file open. On Unix-like systems, a deleted file can continue using disk space until the process releases it. Truncating the file avoids that common issue. If you do delete a log file and disk usage does not fall, check for deleted files that are still open before restarting anything.
Prevent PM2 Logs from Filling Your Disk
Clearing logs solves the immediate problem, but it does not limit future growth. Applications that write frequent messages, debug output, or repeated errors can refill the files quickly. For a long-running server, configure log rotation so older output is rotated or removed automatically.
PM2 offers a log rotation module. You can install it with:
pm2 install pm2-logrotate
After installation, the module can rotate PM2 logs based on settings such as file size, retention, compression, and rotation schedule. For example, you can set a maximum file size and the number of rotated files to keep:
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 7
These example values are not universal recommendations. Choose a size and retention period that fit your disk capacity, traffic, and troubleshooting needs. Check the module’s current configuration options and your installed version before relying on a particular setting. Retaining too few logs can make it harder to investigate an incident, while retaining too many can continue to consume significant storage.
You can inspect the module’s settings with:
pm2 conf
Also review how your application handles logging. Avoid printing secrets, credentials, tokens, or unnecessary personal data. Set an appropriate log level, and make sure frequent messages are useful. Rotation controls file growth; it does not replace sensible application logging or a plan for collecting and retaining important records.
Common Problems When Clearing PM2 Logs
- The log files grow again right away: This is expected if the app continues writing output. Find the source of repeated messages or configure log rotation.
-
pm2 flushreports an error: Confirm that PM2 is installed and available in your shell, and run the command as the user who owns the relevant PM2 daemon. -
The directory appears empty: Check the process’s configured output and error paths, and look for a custom
PM2_HOME. - Disk usage does not decrease after deleting a file: A process may still have the deleted file open. Truncate logs instead where possible, or investigate open deleted files using tools available on your operating system.
- Only one app should be affected: Use the app-specific flush command if supported, or truncate that app’s exact output and error files. Avoid clearing the entire logs directory.
- Logs contain information you need: Save or archive the relevant files before flushing. Clearing logs is destructive and may remove evidence needed for debugging or auditing.
Frequently Asked Questions
Does pm2 flush restart my application? No. It clears PM2-managed log contents and leaves the app running. New log entries can appear immediately afterward.
Does flushing logs remove my PM2 processes? No. It does not remove apps from the PM2 process list. Commands such as pm2 delete have different effects and should not be used just to clear logs.
Can I clear only error logs? Yes. Identify the application’s error-log file and truncate that file alone. The default naming pattern often includes -error.log, but custom PM2 configurations can use different paths.
Will clearing logs affect my saved PM2 configuration? No. Flushing or truncating log files does not normally change your ecosystem configuration or saved process list. It does permanently remove the log contents you clear, so archive anything you may need.
Conclusion
To clear PM2 logs quickly, run pm2 flush. To target one application, provide its name or ID if your PM2 version supports it, or truncate that app’s specific log files. PM2 logs usually live in ~/.pm2/logs/, though the location can differ with a custom PM2 home or output configuration.
Flushing logs does not stop your apps, but it also does not prevent new output from accumulating. For a lasting fix, configure log rotation, review noisy application output, and keep only the logs you need. Before clearing files, confirm the target path and preserve any records required for troubleshooting or compliance.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.