Deleting keys from shelve

Jp Calderone exarkun at divmod.com
Wed Nov 24 14:29:29 EST 2004


On 24 Nov 2004 11:05:27 -0800, OlafMeding at compuserve.com (Olaf Meding) wrote:
>Why does a shelve file never get smaller?
> 
> I noticed that the shelve file (db.txt, see below) does not change
> (get smaller) when deleting a key.  The key is deleted but the file
> size does not change.  I expected the db.txt file to get smaller.
> 
> def delete():
>   s = shelve.open('db.txt')
>   del s['test3']
>   s.close()
> 
> I am using Python 2.2.1 on MS Windows.  Thanks so much for your help.

  shelve uses anydbm for its underlying storage.  anydbm picks one of a number of actual database modules, so that it can work even on systems where a particular database is not available, since Python has support for several.

  Nice thought.

  In practice, it means you don't know how your database will actually behave.  In your case, I'll hazard a guess: you have gotten a database backed by bsddb, which relies on sleepycat's libdb, which never shrinks its database files.  When keys are deleted from the database, the pages they occupied internally are marked as unused and the next time a key is added, those pages may be reused.  The only way to shrink the database file is to create a new database and copy all existing keys into it, then delete the original.

  Jp



More information about the Python-list mailing list