Delete a Command from Zsh history⚓︎
Summary⚓︎
This article is a copy/paste from a post I found on Hashrocket. The article outlines the basic procedure for deleting a command from the ~/.zsh_history file. This will prevent it from showing up in the terminal history.
How-To⚓︎
A while ago, I restored this site’s production database to a backup captured 24 hours earlier (the reason isn’t important). Now in my terminal history, heroku pg:backups:restore a719 DATABASE_URL -rproduction is just hanging out, ready for me to accidentally smash, sending our production database hurtling back into the past. How do I remove this destructive command from my history?
In my terminal, ZSH, there’s a file called ~/.zsh_history, and a similar one for Bash. To remove the command, open that file and remove the entry from the list.
Open a new terminal window, and the bad command is gone.
To avoid this situation, consider the following setting (thanks Dillon!):
With this, any command preceded by a space is excluded from history. One could alias a destructive command to itself, preceded by a space, and save themselves a headache. Note a weird bug here: the last command appears in history, even if it should be excluded, until another command is entered. Banish it to the ether by running just an empty space as a command.