Your Search Bar For Shrewd Tips

How Do I Clear Zsh History?


Clearing Zsh history can mean deleting commands saved to disk, removing commands from the shell session that is currently open, or preventing future commands from being recorded. These are related but separate tasks. If you clear only the history file while another Zsh session is running, that session may write its in-memory history back to the file later. This guide explains how to clear Zsh history on macOS and Linux, check which history file your shell uses, and avoid common mistakes.

How Zsh History Works

Zsh keeps a history list for each shell session and can save commands to a history file for use in later sessions. The file is commonly named .zsh_history in your home directory, but its location is controlled by the HISTFILE setting. You can check the current value by running:

print -r -- "$HISTFILE"

If this prints a path, that is the file Zsh is configured to use. If it prints nothing, Zsh may not have a history file set in the current session, or your configuration may set one only when a new shell starts. A common default path is ~/.zsh_history, which expands to a file such as /Users/yourname/.zsh_history on macOS or /home/yourname/.zsh_history on Linux.

History settings can change how and when commands are saved. For example, options such as INC_APPEND_HISTORY and SHARE_HISTORY can cause commands to be written or shared while shells are still open. That is why the safest way to clear history is to account for both the saved file and any active Zsh sessions.

How to Clear Zsh History Completely

To clear saved history and prevent the current shell from saving its in-memory commands when it exits, first close any other Zsh sessions that might be using the same history file. This includes other terminal windows, tabs, and shell sessions in editors. Then run the following in the session you want to clear:

histfile=${HISTFILE:-$HOME/.zsh_history}
unset HISTFILE
: > "$histfile"

The first line stores the history file path in a temporary shell variable. If HISTFILE is unset, it uses the common default path instead. The second line unsets HISTFILE, so the current shell will not write its history to that file when it exits. The final line truncates the file, leaving it empty while preserving the file itself.

Afterward, replace the current shell with a new one:

exec zsh

Using exec replaces the current shell process instead of letting that session close normally and save its old history. The new Zsh session reads your startup files, which may set HISTFILE again. If the history file was empty and no other session has written to it, your new session starts without the old saved commands.

If you prefer not to use exec, you can exit after unsetting HISTFILE and clearing the file, then open a new terminal. The important points are to stop the current session from saving its old history and to make sure other active sessions do not repopulate the file.

Clear the Zsh History File Directly

If you only need to empty the saved history file, use the following command:

: > "${HISTFILE:-$HOME/.zsh_history}"

This redirects empty output into the configured history file, truncating it to zero bytes. It is generally preferable to truncating a file with a text editor because it is quick and does not require selecting or deleting its contents manually.

However, truncating the file alone does not clear commands held in the memory of an open shell. A session with history-saving options enabled can write those commands back later. For a reliable cleanup, close other Zsh sessions first, prevent the session you are using from saving its old history, and then truncate the file as described above.

If you know that your history file is in a different location, use that path instead. For example:

: > "$HOME/Documents/custom_zsh_history"

Be careful when entering a custom path: truncating the wrong file can delete data you intended to keep. Check the value of HISTFILE before you clear it.

Clear the Current Session’s History

Zsh keeps a session’s history in memory as well as saving it to disk. There is no need to confuse the two: removing the history file handles saved commands, while ending or replacing the shell session discards that session’s in-memory list. To clear both, prevent the current shell from saving its history, empty the file, and start a fresh session.

The fc command is Zsh’s history-related builtin, and fc -p can start a separate history context. It is not a substitute for clearing the history file, however, and it may not be the simplest choice when your goal is to remove every saved command. For a full reset, use the file-clearing method and restart the shell.

Remember that a terminal emulator may still display earlier commands in its scrollback even after Zsh history has been cleared. Scrollback is stored by the terminal application, not necessarily in the Zsh history file. Use the terminal’s clear-scrollback feature if you also want to remove visible output from the window.

Delete One Command from Zsh History

If you want to remove only a particular command, close the other Zsh sessions first and edit the history file with a text editor. Locate the relevant entry, delete it, and save the file. The history file may contain timestamps or extended-history formatting, so a command can appear with additional text before it. Some commands may also span multiple lines.

After editing the file, restart active shells or otherwise ensure they do not write their older in-memory history back to disk. If you leave a session running, the removed command could reappear when that session saves its history.

Avoid relying on a simple text search-and-delete command for sensitive entries unless you understand your history format. A search can match more than one entry, fail to account for multiline commands, or modify the wrong file. For a small number of entries, manually reviewing the correct history file is often easier to verify.

Stop Zsh from Saving Future Commands

To stop the current shell from saving history to a file, unset the HISTFILE variable:

unset HISTFILE

This affects the current shell session. It does not necessarily change your Zsh configuration, so a new shell may set HISTFILE again and resume saving history. If you want to disable history saving in future sessions, review your Zsh startup files, such as ~/.zshrc, for a line that sets HISTFILE and for history-related options.

You can also configure Zsh to ignore commands that begin with a space, but this works only when the relevant option is enabled. Add this to ~/.zshrc if you want that behavior:

setopt HIST_IGNORE_SPACE

Then begin a command with a space when you do not want it recorded. This is a convenience, not a guaranteed privacy measure: the option must be active, and other shell tools or system logs may record activity independently.

Common Mistakes When Clearing Zsh History

  • Deleting only the file: An active shell may still have old commands in memory and write them back.
  • Assuming every system uses the same path: Check HISTFILE instead of assuming the file is always ~/.zsh_history.
  • Using Bash instructions in Zsh: Commands such as history -c are commonly associated with Bash and may not clear Zsh history as expected.
  • Forgetting shared sessions: Another terminal window or editor can save commands to the same history file after you clear it.
  • Confusing history with terminal scrollback: Clearing Zsh’s history does not automatically erase text already displayed in your terminal.

Does Clearing Zsh History Securely Erase Commands?

Clearing or truncating .zsh_history removes entries from the active history file, but it does not guarantee that every trace of a command is gone. Copies may exist in backups, cloud-synced folders, filesystem snapshots, terminal logs, editor logs, or other monitoring tools. On modern SSDs and filesystems, ordinary deletion or truncation may also not securely overwrite the underlying storage.

If a command contained a password, access token, or other secret, treat the secret as potentially exposed. Change or revoke it rather than relying only on history cleanup. To reduce future exposure, avoid typing secrets directly into commands when a safer prompt or credential manager is available.

Conclusion

To clear Zsh history, identify the active history file, close other sessions that could write to it, unset HISTFILE in the session you are clearing, truncate the file, and start a fresh shell. Simply deleting ~/.zsh_history may not be enough if an open session saves its in-memory history later. For ongoing privacy, review your Zsh startup settings and remember that shell history is only one place where command activity may be stored.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →