If you use Zsh on Linux, your command history can be useful—but it may also contain private commands, paths, or other information you would rather not keep. To clear Zsh history, you need to consider both the history file on disk and the history held by running shell sessions. Clearing only one may leave commands visible or cause them to be saved again. This guide explains how to find your Zsh history file, remove its contents, clear history safely, and prevent future commands from being recorded.
Where Does Zsh Store History on Linux?
Zsh normally stores saved command history in a file named .zsh_history in your home directory. For a typical user, the path is ~/.zsh_history. However, Zsh can be configured to use a different file, so check the value of HISTFILE before deleting anything.
print -r -- "$HISTFILE"
If the command prints a path, that is the history file configured for your current shell. If it prints nothing, Zsh commonly uses ~/.zsh_history as its default. Your setup may also use options such as SHARE_HISTORY or INC_APPEND_HISTORY, which affect when commands are written and how multiple shells share them.
History is not limited to the file. Each running Zsh process can also keep commands in memory. That is why deleting ~/.zsh_history while other Zsh terminals remain open may not be enough: an active shell could write its in-memory history back to the file later.
The Safest Way to Clear Zsh History
The most reliable approach is to close all Zsh sessions, clear the history file, and then open a new shell. This prevents a different terminal from restoring its older in-memory history after you have removed the file contents.
- Save any work and close every terminal or application window running Zsh.
- Open a shell that is not Zsh, such as Bash, or use another suitable terminal session.
- Check the location of the history file you intend to clear.
- Empty that file, then start a fresh Zsh session.
For the default history file, you can empty it with:
: > "$HOME/.zsh_history"
This truncates the file: it removes its contents while leaving the file itself in place. If your Zsh configuration uses a custom history path, replace $HOME/.zsh_history with the path shown by HISTFILE. Afterward, start a new Zsh session. Its history should be empty, apart from commands you run from that point onward.
You can also remove the file instead of truncating it:
rm -- "$HOME/.zsh_history"
Zsh may create a new history file when it next saves history. Truncating is often simpler because it preserves the existing file and its permissions. You do not normally need sudo to clear your own history file. Using elevated privileges can accidentally change file ownership or affect another user’s history.
Clear History While Your Current Zsh Session Is Open
If you cannot close your current terminal first, take care to stop that shell from writing its old in-memory history back to the file. The following example records the configured history path, empties it, and changes the current shell’s history destination to /dev/null before you exit:
histfile=${HISTFILE:-$HOME/.zsh_history}
: > "$histfile"
HISTFILE=/dev/null
exit
Run these commands in the Zsh session whose history you want to clear. The first line saves the history-file path in a temporary shell variable. The second truncates that file, and the third directs any history saved by this shell on exit to /dev/null. Then open a new Zsh session.
This method does not automatically clear history held by other open Zsh sessions. Those sessions may still write their own history to the original file. For a complete cleanup, close or handle every active Zsh session before clearing the file. If you are unsure which terminals are using Zsh, the close-all-sessions method is easier to verify.
How to Check That Zsh History Was Cleared
After opening a fresh Zsh session, inspect the history file:
ls -l "$HOME/.zsh_history"
cat "$HOME/.zsh_history"
If you use a custom file, substitute its path. An empty file should show no command entries. You can also run history in the new session to view the commands currently available to that shell. Remember that the command you just ran may appear in history after it is recorded, so seeing new commands does not necessarily mean older entries returned.
If the file becomes populated again with old commands, another shell may still be running, or your Zsh configuration may set a custom HISTFILE. Check the configuration files that apply to your setup, including ~/.zshrc, and look for assignments to HISTFILE and history-related options.
Can You Use history -c in Zsh?
Many Bash guides recommend history -c to clear the current session’s history. Zsh’s history commands and options differ, so do not assume that Bash instructions work the same way in Zsh. Depending on the command and Zsh version, a familiar Bash option may not behave as expected or may not be available.
For a dependable result, clear the file that Zsh uses and make sure running shells cannot write old entries back. Restarting Zsh after clearing the file is a straightforward way to get a clean session. If you need to manage history in a specialized Zsh setup, consult the Zsh documentation for the commands and options supported by your installed version.
How to Stop Zsh from Saving Future Commands
If you want to prevent future commands from being saved, rather than clear history just once, you can adjust your Zsh configuration. One simple option is to set HISTFILE to /dev/null for the current session:
HISTFILE=/dev/null
To apply that setting whenever Zsh starts, add the assignment to ~/.zshrc:
HISTFILE=/dev/null
Then reload the configuration or open a new Zsh session. Be aware that this disables normal persistent command history, which can be inconvenient if you rely on history search or the up-arrow key to find commands from previous sessions.
Another approach is to set the history size to zero, but the exact effect depends on your Zsh configuration and history options. If you choose to change history settings, review your existing HISTSIZE, SAVEHIST, and history-related options in ~/.zshrc. Keep a copy of the original configuration so you can restore history later.
Important Privacy and Security Considerations
Clearing Zsh history removes entries from the configured history file; it does not erase every possible record of a command from your system. A command could also appear in terminal scrollback, backups, copied files, monitoring tools, or other logs. If a command included a password, API key, or other credential, treat that secret as exposed and change or revoke it rather than relying on history cleanup alone.
Deleting or truncating a history file is also not a guarantee of secure, permanent data erasure. Filesystem snapshots, backups, solid-state drives, and storage features such as copy-on-write can retain older data. If you have a specific security requirement, follow the storage and backup procedures appropriate for your device and organization.
Finally, avoid pasting sensitive credentials directly into interactive commands where possible. Use a password manager, a secure prompt, or an environment-specific secret-management tool instead. Preventing sensitive information from entering command history is usually safer than trying to remove it afterward.
Common Problems When Clearing Zsh History
-
The history file is not in the home directory: Check
HISTFILEand clear the configured path rather than assuming the default. - Old commands return after cleanup: Close other Zsh sessions before truncating the file; an open shell may save its in-memory history later.
- The file is recreated: Zsh may create a new file when it saves new commands. A newly created, small history file is normal.
-
You cannot write to the file: Check that you are using your own history file and that you have permission to change it. Avoid using
sudounless you are intentionally managing a different account. - A Bash command does not work: Zsh and Bash do not have identical history behavior. Use Zsh-specific documentation or the file-and-restart method described above.
Conclusion
To clear Zsh history on Linux, first find the history file using HISTFILE, then empty the configured file. For the most reliable cleanup, close all Zsh sessions before truncating the file and launch a fresh shell afterward. If you clear history from an open session, prevent that session—and any other active shells—from saving old entries back. For sensitive credentials, rotate the secret as well: removing a command from Zsh history cannot guarantee that no other copy exists.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.