Clear db in python

I’ve made a database to store URLs and titles of various posts, with the URL being the key. Unfortunately I now can’t access any of the keys since they are URLs (according to this post:
Replit DB raises KeyError for a key that exists).

My typical way of emptying the db is:

for key in db.keys():
  del db[key]

This returns a KeyError.

Is there any way to clear the database in python without calling on keys, or is my only option to fork it?

Just use db.clear()

1 Like

Unfortunately that also doesn’t seem to work. I guess I’ll just fork it.

Hey @BrynParry

You can use this in the shell → replit nuke --i-am-sure . It removes all the values in your db (it doesn’t delete anything else just clears your DB).

4 Likes

I just realized how ridiculous this sounds, replit nuke sounds a lot more like destroying your Repl than simply clearing your database, but that’s just me :laughing:

5 Likes

Believe it or not, that also produces a KeyError…

(Sorry for the image, I can’t figure out how to copy from the console/shell either)

Could you send a link to you repl?

A keyerror indicates that the program tried to access an item in the db that does not exist. For example, if you tried to access db["nonExistentUser"] when nonExistententUser doesn’t exist yet, then you would get KeyError: nonExistendUser. In your case, the program found an item that was called https://ask.replit.com/t/getting-captcha-in-100daysofcode-90th-day/54400, but then the program also couldn’t access that item. In this case, i would enter the python console, and run the following code:

from replit import db
print(db.keys())

and share the output with us (as long as it doesn’t contain any confidential information)

2 Likes

Thanks for the response! Unfortunately I just forked and deleted the original repl. The keys I was using were definitely in the DB since to get them I was using:

for key in db.keys():
 del db[key]

I think it’s because they were a url and that causes problems apparently. Going to re-make it but using a different key.

2 Likes

sounds cool, I’m glad that you got a solution!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.