for key in list(db):
del db[key]
should work.
EDIT: If the goal is just to empty the database completely, you can also close the database and re-open it with dbm.open('filename', 'n')
. The 'n'
flag means "Always create a new, empty database, open for reading and writing"; it seems to override any previously-existing file.