Clear all entries in Docker mongodb?

13,950

Solution 1

There are a number of ways:

  • The same way you would normally clear a mongodb database. You're running it in Docker, but it is a real mongodb after all. See this, and this.
  • If you've mounted your database's data as a volume on the host system using -v on the docker run command, you can just clear this folder out.
  • If you haven't, the data lives in the container. You can remove the container and recreate it (docker rm container_name). Or run a shell in the container and remove the data from the container's filesystem (docker exec -it container_name bash).

Regarding the last option, you shouldn't be in this scenario because your data should live on the host system and your container should be disposable.

Solution 2

docker exec -it mongodb_container bash -c "mongo db-name --eval 'db.dropDatabase()'"
Share:
13,950
Cisplatin
Author by

Cisplatin

Updated on July 20, 2022

Comments

  • Cisplatin
    Cisplatin almost 2 years

    Is there an easy way to clear a mongodb database running in docker?

    Or possibly delete it all and create it again?

  • obesechicken13
    obesechicken13 about 6 years
    You can also delete volumes using docker volume rm <volume_name>