To clear your terminal history on a Mac, you use the history -c command to wipe the current session’s list and delete the .bash_history or .zsh_history file to remove saved commands. This guide covers both the quick commands and the deeper file-level cleanup, plus how to prevent future commands from being saved.
Why Your Mac Saves Terminal Commands
When you type a command in Terminal and press Return, the shell (the program that interprets your commands) stores that line in a history file. This is a convenience feature: you can press the Up arrow to recall a previous command, search with Ctrl+R, or rerun a long command without retyping it. The default shell on macOS since Catalina (10.15) is zsh, which saves history to ~/.zsh_history. Older Macs using bash save to ~/.bash_history.
The history file is a plain text file in your home directory, hidden by default. It grows as you work, and it can contain sensitive information like passwords typed as arguments, API keys, or file paths you’d rather not leave behind. Clearing it is a straightforward privacy and housekeeping task.
Method 1: Clear Current Session History with history -c
The fastest way to wipe the in-memory history for the current Terminal window is the history command with the -c flag:
- Open Terminal (found in Applications → Utilities).
- Type
history -cand press Return. - Type
historyand press Return to verify. You should see an empty list.
This clears the history for the current shell session only. If you open a new Terminal window, the shell will read the history file again and your old commands will reappear. To fully remove saved commands, you must also delete the history file as described below.
Method 2: Delete the History File Permanently
To erase all saved commands across all sessions, delete the history file. The command differs depending on your default shell:
- For zsh (macOS Catalina and later):
rm ~/.zsh_history - For bash (older macOS or if you switched):
rm ~/.bash_history
After deletion, the shell will create a new empty history file the next time you run a command. You can also clear the file without deleting it entirely using > ~/.zsh_history (the > redirects nothing into the file, truncating it). This is useful if you want to keep the file but start fresh.
Method 3: Clear Specific Commands from History
If you only want to remove one or a few commands rather than the entire history, use the -d flag with a line number. First, view your history with line numbers:
history
Each entry is numbered. To delete entry 42, run:
history -d 42
You can also delete a range by specifying start and end: history -d 42 45 removes entries 42 through 45. This is handy when you accidentally typed a password or a sensitive path and want to erase just that line.
Method 4: Prevent Future Commands from Being Saved
If you want to stop Terminal from recording your commands entirely, you can change your shell configuration. For zsh, edit the ~/.zshrc file:
nano ~/.zshrc
Add this line:
unsetopt HISTFILE
This tells zsh not to write a history file. For bash, edit ~/.bashrc or ~/.bash_profile and add:
unset HISTFILE
After saving, restart Terminal. Note that this only prevents future commands from being saved; it does not erase existing history. You still need to delete the current history file as in Method 2.
For a one-time omission, you can prefix a command with a space (in zsh, if the HIST_IGNORE_SPACE option is set) or use unset HISTFILE in the current session. However, the simplest approach is to run history -c and then delete the file.
Common Mistakes and Misconceptions
A frequent error is running history -c and assuming your saved history is gone. As noted, it only clears the current session’s buffer. Another misconception is that deleting .zsh_history will break Terminal. It won’t—the shell recreates the file automatically. Some users try rm -rf ~/.zsh_history which works, but the -rf flags are unnecessary and potentially dangerous if you mistype the path.
Another issue: if you use iTerm2 or another terminal emulator, the history is still managed by the shell, so the same commands apply. However, if you use tmux or screen, each session may have its own history, and you need to clear each one.
Finally, note that commands you run with sudo are stored in the root user’s history if you switch to root, not in your own. To clear root’s history, you’d need to run sudo history -c or delete /root/.zsh_history (requires admin privileges).
Alternatives to Clearing History
Instead of wiping your entire history, you can selectively ignore sensitive commands. In zsh, you can set the HIST_IGNORE_SPACE option so that commands beginning with a space are not recorded. Add this to ~/.zshrc:
setopt HIST_IGNORE_SPACE
Then, when typing a command you want to keep private, prefix it with a space. For example, mysecretcommand (note the leading space) will not appear in history. In bash, the equivalent is HISTCONTROL=ignorespace in your ~/.bashrc.
Another alternative is to use the fc command to edit and re-execute history entries, or to simply not worry about it if you’re on a personal machine. But for shared or work computers, clearing or ignoring history is a good habit.
Frequently Asked Questions
Does clearing Terminal history affect my shell settings?
No. Your .zshrc or .bashrc files (which contain aliases, environment variables, and startup commands) are separate from the history file. Deleting .zsh_history or running history -c will not change your shell configuration or break anything.
Can I recover deleted Terminal history?
Once you delete the history file, the commands are gone unless you have a Time Machine backup or use a file recovery tool. There is no built-in undo for history -c or rm. If you need to keep a record, consider copying the history file to a safe location before clearing.
Why does my history still show commands after I deleted the file?
If you deleted the file while Terminal was open, the current session still holds the commands in memory. Run history -c to clear the in-memory list, then close and reopen Terminal. The new session will have no history because the file is gone.
What is the difference between history -c and rm .zsh_history?
history -c clears only the current session’s in-memory list. rm ~/.zsh_history deletes the file that stores commands across sessions. For a complete wipe, you need both: run history -c and then delete the file (or delete the file first and then close the terminal).
Does clearing history improve performance?
Not noticeably. The history file is small (usually a few kilobytes to a few hundred kilobytes). Clearing it won’t speed up Terminal. The main reasons to clear are privacy and tidiness.
Can I clear history for all users on the Mac?
You can clear your own history easily. To clear another user’s history, you’d need admin access and would edit their home directory, e.g., sudo rm /Users/otheruser/.zsh_history. That is generally not recommended unless you have explicit permission.
Related
Formula used
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Secure Boot Battlefield 6 ASUS: Fix & Enable Guide
- Best Tech Websites to Follow for Pros & Beginners
- Second Wind Quest Walkthrough: Where Winds Meet6.00 min/km
- Enshrouded Weapon Upgrade Guide: How to Improve Gear5.00 min/km
- Connect Phone to Controller for Cloud Gaming
- Python Search Highlighting: Snippets & Matches