Clear Terminal History in Mac

In this guide, we will explain how to clear your command history in the Mac Terminal using commands, and other ways.

Introduction to Terminal History

When you use the Terminal on your Mac, it keeps a record of every command you execute. This history is stored even after you close the Terminal, allowing you to recall previous commands quickly using the up and down arrow keys. While this is convenient, there may be times when you want to clear your Terminal history for privacy reasons or to prevent clutter.

On macOS, Terminal saves the command history in a hidden file that you can manage manually or using commands.

Method 1: Clear Terminal History Using “history -c”

The easiest way to clear your current session’s Terminal history is by using the history command with the -c flag. This will clear the command history in the current Terminal window, but it may not delete history from previous sessions saved in the history file.

Follow these steps:

Step 1: Open the Terminal app on your Mac.

Step 2: Type the following command and press Enter:

history -p

Step 3: This command will clear the history for the current Terminal session.

Clear Terminal History

Step 4: To prevent these commands from being saved, close the Terminal window or type exit.

Method 2: Clear History from the .bash_history File

The Terminal stores history from past sessions in a hidden file called .bash_history (for Bash shell users) or .zsh_history (for Zsh shell users). Deleting this file will permanently clear all saved command history.

Follow these steps:

Step 1: Open the Terminal app.

Step 2: To remove the .bash_history file, type the following command and press Enter:

rm ~/.bash_history

Step 3: If you are using the Zsh shell, type the following command instead:

rm ~/.zsh_history

Step 4: After removing the file, close the Terminal and reopen it to start with a new session.

Method 3: Prevent Terminal from Saving History

If you want to prevent Terminal from saving any future commands, you can disable history logging temporarily. This is helpful if you don’t want to save specific commands but still want history for other sessions.

Step 1: Open the Terminal app.

Step 2: To prevent saving commands for the current session, type:

unset HISTFILE

Step 3: This command disables history logging until you close the current session. Once you restart the Terminal, history will resume being saved unless you run the command again.