How do I make shelve file empty in python?

Abhishek Kulkarni picture Abhishek Kulkarni · Jun 27, 2013 · Viewed 8.7k times · Source

I have created a shelve file and inserted a dictionary data. Now I want to cleanup that shelve file to reuse as a clean file.

import shelve
dict = shelve.open("Sample.db")
# insert some data into sample.db
dict = { "foo" : "bar"}

#Now I want to clean entire shelve file to re-insert the data from begining.

Answer

Dima Tisnek picture Dima Tisnek · Jun 27, 2013

Shelve behaves just like a dictionary, thus:

dict.clear()

Alternatively, you can always remove the file and let shelve create a new one.