If you want to clear Zsh command history, the key is to remove it from the history file and prevent any open Zsh sessions from writing old entries back. Zsh keeps history both in memory while a shell is running and, usually, in a file such as ~/.zsh_history. Clearing only one may leave the other intact. This guide explains how to find your Zsh history file, clear saved history safely, avoid common mistakes, and reduce the chance that deleted commands reappear.
How Zsh Command History Works
Zsh history has two parts: the commands available to the current shell session and the commands saved to a history file. The shell stores settings such as the history file path and the number of entries to keep in variables including HISTFILE, HISTSIZE, and SAVEHIST.
By default, the history file is commonly ~/.zsh_history, but your configuration may set a different location. Zsh can also append commands as you run them or share history between open sessions. As a result, truncating the file while other Zsh windows are still open may not be enough: those sessions can write their in-memory history back to disk.
Find Your Zsh History File
Before clearing history, check which file your shell uses. In Zsh, run:
print -r -- "$HISTFILE"
If the command prints a path, that is the current shell’s history file. If it prints a blank line, check the usual default location:
ls -l ~/.zsh_history
Your Zsh startup files, such as ~/.zshrc, may define a custom HISTFILE value. If the path is different from ~/.zsh_history, use the configured path in the steps below.
Clear All Saved Zsh History
The most dependable approach is to close every Zsh session first, then truncate the history file from a separate shell or after logging in again with a shell that does not use that file. Closing the sessions matters because a running Zsh shell may still hold history in memory and save it when it exits.
- Close all terminal tabs, windows, and remote sessions running Zsh.
- Open a shell that is not using the same Zsh history file, or use a fresh session after the old ones have closed.
- Truncate the history file. For the default file, run:
: > "$HOME/.zsh_history"
The : command does nothing, while the redirection empties the file without deleting it. If your history file is at a different path, replace $HOME/.zsh_history with that path. For example:
: > "$HOME/.config/zsh/history"
To confirm that the default file is empty, run:
wc -c "$HOME/.zsh_history"
A result of 0 bytes means the file is empty. Start a new Zsh session afterward. The new session will begin recording history again if history is enabled.
Why You Should Close Other Zsh Sessions First
Zsh may save history when a session exits, and some configurations append or share entries while sessions are running. If you empty the file while another terminal is open, that terminal may later save commands that were in its memory. The history file can then contain entries you expected to remove.
For the same reason, running : > "$HISTFILE" inside a long-running Zsh session is not always a complete cleanup. It empties the file at that moment, but the current shell or another shell may write history to it afterward. If you cannot close every session, be aware that the result may be temporary. For a reliable cleanup, close all sessions that use the file before truncating it.
Clear History and Keep It From Being Saved
If your goal is not just to remove old entries but also to prevent future commands from being saved, change the history settings for the relevant session. You can disable common append and sharing options with:
unsetopt APPEND_HISTORY INC_APPEND_HISTORY INC_APPEND_HISTORY_TIME SHARE_HISTORY
These options affect how history is written or shared; they do not erase existing entries from the history file. The exact options enabled on your system depend on your Zsh configuration. You can check relevant settings with:
setopt | grep -E 'history|append|share'
You can also temporarily direct a shell’s history file somewhere else by setting HISTFILE. For example, assigning it to /dev/null prevents that session from writing a normal history file:
HISTFILE=/dev/null
This is a session-level setting unless you add it to a startup file. Be cautious about making permanent changes: disabling history can make it harder to retrieve useful commands later. Also, changing these options does not remove history already stored in other open sessions or saved elsewhere.
Does history -c Work in Zsh?
Many instructions for clearing command history use history -c, a familiar Bash command. Zsh’s history interface is different, so Bash instructions and flags should not be assumed to work the same way in Zsh. In particular, do not rely on history -c as a universal way to erase both the current session’s entries and the saved Zsh history file.
For a dependable cleanup, identify the file Zsh uses, close sessions that could write to it, and truncate that file. Then open a fresh session. This approach addresses the persistent history file directly rather than depending on a shell-specific history shortcut.
Can You Delete Only One Zsh History Entry?
If you want to remove only one command, first identify where it is stored. The command may be present in the current session’s memory, the history file, or both. Zsh history files can also use an extended format containing timestamps and other metadata, so editing them casually can damage the file or remove more than intended.
A cautious file-editing workflow is to close all Zsh sessions, make a backup, and edit the configured history file with a text editor. For example:
cp "$HOME/.zsh_history" "$HOME/.zsh_history.backup"
nano "$HOME/.zsh_history"
Replace the example path if your HISTFILE points elsewhere. Find the unwanted entry, remove the complete corresponding record, save the file, and then start a new Zsh session. If the file contains extended history records, inspect the surrounding lines carefully before editing. A backup gives you a way to restore the original if the edit removes the wrong entry.
For users who do not need to preserve other commands, clearing the entire history file is usually simpler and less error-prone than editing individual records.
Common Problems When Clearing Zsh History
- The commands come back: Another Zsh session may have written its in-memory history to the file. Close every session that uses the same history file and repeat the cleanup.
-
The file is empty, but history still appears: The current shell may still have commands in memory, or Zsh may be using a different history file. Check
$HISTFILEand open a fresh shell. - The file path is blank: Your shell may not have a history file configured, or the variable may not be set in that session. Check your Zsh startup files and the default path.
-
A command is saved immediately: Options such as
INC_APPEND_HISTORYorSHARE_HISTORYcan affect when entries are written. Close sessions before truncating the file. - Permissions prevent truncation: Check that you own the file and have permission to write to it. Avoid using elevated privileges unless you understand why they are needed.
Does Clearing Zsh History Securely Erase Commands?
Truncating ~/.zsh_history removes its current contents, but it is not a guarantee that sensitive information has been securely erased from every place it may exist. Copies may remain in backups, filesystem snapshots, cloud-synced folders, terminal logs, or other records. Storage devices may also handle deleted or overwritten data in ways that prevent a simple shell command from guaranteeing permanent removal.
If a command contained a password, access token, private key, or other secret, treat the secret as potentially exposed. Change or revoke it using the service that issued it. Going forward, avoid typing secrets directly into commands that may be recorded, and use a dedicated secret-management method when appropriate.
Conclusion
To clear Zsh command history, first find the history file with print -r -- "$HISTFILE". Close every Zsh session that could write to it, then truncate the file, for example with : > "$HOME/.zsh_history" when using the default path. Start a fresh shell to confirm the saved history is gone. Remember that clearing the file does not automatically erase commands held in open sessions or copies stored elsewhere, so session management is an essential part of a complete cleanup.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.