Clearing your Zsh history on a Mac removes commands that your shell has saved, but the process is not always as simple as deleting one file. Zsh can keep history in memory while a Terminal window is open, and multiple open shells may write their history back to disk. This guide explains how to clear the saved history safely, check where it is stored, and avoid having old commands reappear.
Where Does Zsh Store History on a Mac?
Zsh is the default shell on current versions of macOS. By default, it saves command history in a hidden file in your home folder named .zsh_history. The full path is usually:
~/.zsh_history
The tilde (~) represents your home folder. The file is hidden in Finder because its name begins with a dot. Zsh also keeps a history list in each running shell session, so the file on disk is only part of the picture. Options such as shared history can cause open Terminal or iTerm2 sessions to exchange or write history as you work.
Before You Clear Zsh History
First, decide whether you want to remove all saved commands or just prevent future commands from being recorded. If you are clearing history because a command contained a password, token, or other secret, remember that shell history may not be the only place where that information exists. Your terminal may still show it in scrollback, and other applications or tools may have their own logs.
It is also important to close other shell sessions before clearing the history file. An open session may still have older commands in memory and write them to the file later. Close other Terminal and iTerm2 windows and tabs, and check for other apps or remote sessions running Zsh. Keep one session open to perform the clearing steps below.
How to Clear Zsh History Completely
The following method truncates the active history file and then immediately terminates the shell. Terminating it prevents that shell from performing its usual exit-time history save, which could otherwise restore commands from memory. Because the shell is terminated, save any work in that Terminal session first.
Close other Terminal or iTerm2 tabs, windows, and sessions that are running Zsh.
-
In the one remaining Zsh session, run this command:
histfile=${HISTFILE:-$HOME/.zsh_history} : > "$histfile" && kill -KILL $$ Open a new Terminal or iTerm2 window. The new shell starts with an empty history file.
The first line uses the configured HISTFILE if one exists, and otherwise uses the standard ~/.zsh_history location. The second line empties that file and, if the operation succeeds, sends a force-termination signal to the current shell. You may see a message that the session ended; that is expected.
Do not run this command while other Zsh sessions are still open if you need a reliable full clear. A remaining session may append its in-memory history after the file has been emptied. If you are unsure whether another shell is running, close Terminal and iTerm2 windows, quit those apps, reopen one, and then perform the procedure.
Check Which History File Zsh Uses
If you have customized your shell, your history file may be in a different location. To see the current setting, run:
printf '%s\n' "$HISTFILE"
If this prints a path, that is the file your current Zsh session is configured to use. If it prints nothing, Zsh may not have a history file set in that session. The usual default is $HOME/.zsh_history. You can also check the size and modification time of the standard file with:
ls -l "$HOME/.zsh_history"
If your configuration sets HISTFILE in ~/.zshrc, make sure you clear the configured file rather than assuming the default path. You can search your Zsh configuration for history settings with:
grep -nE 'HISTFILE|SAVEHIST|HISTSIZE' ~/.zshrc ~/.zprofile 2>/dev/null
These settings can reveal where history is saved and how many commands Zsh keeps. Take care not to share the output of configuration files publicly if they contain other private information.
Does the clear Command Delete Zsh History?
No. The clear command clears the visible Terminal screen, but it does not erase commands from Zsh’s history file or the active shell’s history list. On many Mac terminal apps, pressing Command-K clears the window’s scrollback, but that also does not delete Zsh history. History, the visible screen, and the scrollback buffer are separate things.
To clear just the visible screen, you can run:
clear
Use the full history-clearing procedure when your goal is to remove saved commands. If your concern is that someone could scroll back and see text in the terminal window, clear the terminal’s scrollback as a separate step after clearing history.
How to Stop Zsh from Saving Future Commands
To prevent future commands from being saved, you can disable history for the current session by unsetting the history file and turning off history options:
unset HISTFILE
unsetopt APPEND_HISTORY INC_APPEND_HISTORY INC_APPEND_HISTORY_TIME SHARE_HISTORY
This change applies to the current shell session. If you open a new window, your configuration may turn history back on. To make a change persist, edit your Zsh startup configuration, commonly ~/.zshrc. For example, you can add:
unsetopt APPEND_HISTORY INC_APPEND_HISTORY INC_APPEND_HISTORY_TIME SHARE_HISTORY
Then start a new shell or reload the configuration with source ~/.zshrc. Be aware that turning off these options changes how Zsh handles history, but it does not automatically erase commands already written to the history file. Clear the existing file separately if you want to remove old entries.
You can also control how much history Zsh retains with settings such as HISTSIZE and SAVEHIST. These settings limit the number of commands kept in memory or saved to disk; they are not a substitute for clearing the history file.
What If Old Commands Come Back?
If commands reappear after you clear the file, another session or configuration may be writing them back. Try these checks:
Close every other Terminal, iTerm2, and remote shell session before clearing the file again.
Confirm the active history file by checking the value of
$HISTFILE.Look for shared-history settings, including
SHARE_HISTORYandINC_APPEND_HISTORY, in your startup files.Check whether a plugin, shell framework, or custom startup script changes the history file or history options.
After clearing history, open a fresh shell and inspect the file again rather than relying on the old session.
Zsh configurations vary, so history behavior can differ between Macs or even between terminal windows on the same Mac. If you use a custom Zsh framework or manage settings through a dotfiles repository, review those settings before making a persistent change.
Does Clearing Zsh History Remove Secrets Everywhere?
No. Clearing .zsh_history removes saved shell commands from that history file, but it does not guarantee that a secret is erased from every location. A command may remain visible in Terminal scrollback, copied into the clipboard, recorded by a terminal app, or stored in another application’s logs. If you entered a password or access token directly into a command, consider replacing or revoking it as well as clearing the history.
For future commands that involve sensitive information, avoid placing secrets directly in the command line when possible. Command-line arguments can be exposed through history, process listings, logs, or other system tools. Use a secure prompt or a dedicated secrets manager when a tool supports one.
Conclusion
To clear Zsh history on a Mac, close other shell sessions, truncate the active history file, and prevent the current shell from saving its in-memory history when it exits. The standard history file is ~/.zsh_history, but checking $HISTFILE helps confirm the correct location. Remember that clearing shell history is different from clearing Terminal’s screen or scrollback, and disabling future history does not erase commands already saved. If old commands return, check for another open session or a custom history setting.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.