Like most command line interfaces, the Terminal application on Mac uses a shell environment to translate the commands you type so the computer can understand them. One great feature of almost any UNIX/LINUX-based shell environment is that it saves your command history.
Mac Terminal’s Z shell saves your command history, and in some situations, you will want or need to clear that history. Whatever the reason, there are a few simple ways to clear it. To clear Terminal’s history on your Mac, you can use the history command, delete the history directory, or set history size to 0.
My name is Eric, and as a software engineer, I use command line interfaces like Mac Terminal all the time. As a frequent user, I often rely on the command history stored by my shell environment, but there are times when I need a fresh start and want to clear it.
If you would like to see how easy it can be to clear your history and discuss some reasons why you may need to do so, keep reading below. We will also discuss how to set your history’s size in case you want to increase or decrease it.
Clear Command History
For our purposes here, we are discussing clearing the shell command history, which is different from the input and output on your Terminal screen, which you can also clear.
You can see the command shell history by typing history and hitting return. It will show you a list of commands you have typed.
The command history also lets you pull commands from your history to your command prompt by hitting the up arrow to move back through the history and the down arrow to move forward through it. This can come in very handy for commands that you run repetitively.
Just use the arrow key to bring the command back to the command prompt and then hit enter. No typing is required. If needed, you can modify the parameters of that command by using your backspace or arrow keys and editing them.
The command history can help us be more efficient and run commands quickly, but there are times when we need to clear the history. For Z shell, there are three ways that we can clear it. Let’s go through each of them.
Clear Using the History Command
In my opinion, the quickest and cleanest way to clear Terminal’s history is to use the history command with the -p (for purge) parameter. No need to find a file or anything else. Just run the command as shown below.
(note: For bash and many other shells, the command is history -c)
Hit your return key, and the command history will clear.
Clear By Deleting the History Directory
If you like to use more technical solutions, you can delete (or rename if you want to save it for any reason) the directory that holds the command history data. For our example, we are using Z shell, so the directory we are looking for is called .zsh_sessions.
Other shells will use a file instead of a directory. The file is usually named by the shell type and then _history. For our case, we are just going to go over Z shell since that is what most Mac systems are using now.
This is a hidden directory, so if you are looking for it, you will need to use the -a parameter with your ls command to see it. It will be located in your home directory. Use the following steps to rename the history file.
Step 1: Go to your home directory
To get to your home directory, type the following command and then hit return.
Step 2: Look for the history directory.
The directory we are looking for is called .zsh_sessions. Run the command below to see if it is there.
Step 3: Remove or rename the .zsh_sessions directory.
I recommend just renaming the folder, which we will do below. That way, you have a copy of it if you need it, but you may delete it if you like. It will not cause any harm to your system since a new one will be generated.
mv .zsh_sessions .zsh_sessions.old
Step 4: Exit your terminal session.
Type the exit command to exit the session and hit the return key.
When you exit, you will see some failure messages indicating the folder is missing—no need to worry about these. The folder will be regenerated when you start Terminal back up again.
Step 5: Close the Terminal application.
Step 6: Start the Terminal application.
Start Terminal using your preferred method.
Step 7: Type the history command and you will see the history is now clear.
Type the history and hit return.
You can see the history is cleared. It only contains the history command that you just ran.
Set History Size to 0
One more method of clearing your history is to set your history size to 0. Once set to 0, the only history you will see is the history command itself when you run it. If you set the history size back to its original setting, all previous history is cleared.
Use the following steps to clear your history using the history size method.
Step 1: Check the current history size
You will want to check the current size so that you can set it back to the original size at the end.
Keep note of the current size. My size shown above is 2000.
Step 2: Set the size to 0.
Step 3: Set the size back to its original value
You will now see that the history has been cleared, and the only command in the history is the last export command you ran.
Why Clear Terminal History
Having your command history available is a great benefit. It allows you to see what commands you have run and quickly go back and rerun or reuse parts of commands. So why would I want to delete the history?
There are a variety of reasons you may need to do this. For example, if you are trying out a new process with a set of commands, you might want to clear everything first so that you can easily go back and see what you did for just that process.
There could also be security or privacy reasons. You might not want someone else to be able to log in and see what commands you have run.
Adjust the History Size
As you can see in the last method we have used, you can set your history buffer’s size to whatever you want. So maybe the solution for your problem is not just to clear the history but to make it smaller.
If you check your history size and find that it is not large enough, you may want to make it bigger. Changing the size of the history buffer is relatively easy, and we have already seen how to do it from the third method we used above to clear history.
All we need to do is set the value of the $HISTSIZE variable in the environment. You can see the current size by typing the following command.
You can then change the size by running the following command. I have changed mine to 3 just for this example, but you can use any size you would like.
As shown above, you can easily clear your command history in Mac’s Terminal application using the history -p command, removing the history directory, or setting the history size to 0. Try all three, and you can decide which method is best for you.
Do you know of other ways to clear the command history? As always, I would love to get your input. Let me know if you have any questions or comments.