Is there a command to clear the history from within the mongo shell?
6 Answers
There should be a file called .dbshell in your home folder. Just delete it.

- 4,359
- 23
- 18
MongoDB terminal history is stored inside the ~/.dbshell
file. Just empty the .dbshell
file
$ > ~/.dbshell
Versions of Windows mongo.exe earlier than 2.2.0 will save the .dbshell file in the mongo.exe working directory.

- 88,237
- 28
- 143
- 153
You can clear contents on the shell by Ctrl + L

- 525
- 5
- 15
-
6As the question was about clearing the history (which also means the previously used commands) on the window and the Ctrl + L option was introduced recently in the latest version of the shell, i thought this would help others to know about it. – praveena_kd Aug 06 '12 at 19:11
-
1@praveena_kd...I found it useful. Exactly what I wanted. +1. – GPGVM May 17 '16 at 21:02
-
Ctrl+L wipes the actual command history now? Not just scrolling the window to hide output history? that's very good to know if so! It doesn't in my shell (Ctrl+R as well as [UP] both retrieve commands) but good to keep in mind. – Kasapo Jun 22 '18 at 18:34
-
1None of the comments thusfar explain why this answer is unhelpful. Ctrl+L clears what you "see" in your shell (scrolls you to the current line and erases scroll history), but leaves the actual history of those commands in tact (you can still up arrow to see them, or Ctrl+r to search them). Only deleting the file on disk removes the history of them. This answer is helpful however if you came to this article trying to answer the question "how do I clear my mongo shell's output" rather than "how do I clear my mongo shell's history". A little disclaimer in the answer would clear things up. – Scott Oct 09 '19 at 15:30
Command history is stored in ~/.dbshell file.
For example, on a Unix-like system:
# Ensure an empty history file
echo "" > ~/.dbshell
# Remove rwx access to the history file
chmod 0 ~/.dbshell

- 15,447
- 5
- 79
- 98
If you're using MongoDB Shell as a standalone package, you will find the shell history file located in ~/.dbshell
If you're using the embedded MongoDB Shell inside MongoDB Compass, you will find the shell history file located in ~/.config/MongoDB Compass/MongoDB Compass/shell-history.json
Just empty the history file

- 151
- 1
- 10
Run:
sudo rm ~/.mongodb/mongosh/mongosh_repl_history
Once that's done, all of your history will have been deleted from the MongoDB Shell.

- 19
- 4