Here is what you do after you accidentally deleted your database

Step 1. Do not panic.
Step 2. If you remember the name of your database. Just create another similar file with the same name and you will get the database back. ( This is NOT guaranteed )
step 3. If you did not manage to get it back/you got it back, heres some measures you CAN/MUST take incase you lose something.

You want to have atleast >2 databases incase you deleted one.
Here is an example:

import json
import os
from replit import db

keys = db.keys()

data = {}
for key in keys:
    data[key] = replit.db[key]

file_path = os.path.join(os.getcwd(), "database_backup.json")

with open(file_path, "w") as f:
    json.dump(data, f, indent=2)

You can use this to store data incase you lose the json file or you accidentally deleted everything in your replit db somehow

heres a proper save() function:

import json
import os
from replit import db
def save():
  keys = db.keys()
  
  data = {}
  for key in keys:
    data[key] = replit.db[key]
  
  file_path = os.path.join(os.getcwd(), "database_backup.json")
  
  with open(file_path, "w") as f:
    json.dump(data, f, indent=2)

#example:
x = input('Test > ')
db['Test'] = x
save()

If you are reading this
Thank you and please keep your databases safe incase someone snitches and deletes your database on purpose/ on accident.
Thank you!

Yeah yeah thank you
its beautiful isn’t it?

Why not just… db.keys()? You imported db

2 Likes