You want to clear out a DBM file.
Open the database and assign
() to it. Use
dbmopen(%HASH, $FILENAME, 0666) or die "Can't open FILENAME: $!\n"; %HASH = (); dbmclose %HASH;
use DB_File; tie(%HASH, "DB_File", $FILENAME) or die "Can't open FILENAME: $!\n"; %HASH = (); untie %HASH;
Alternatively, delete the file and reopen with create mode:
unlink $FILENAME or die "Couldn't unlink $FILENAME to empty the database: $!\n"; dbmopen(%HASH, $FILENAME, 0666) or die "Couldn't create $FILENAME database: $!\n";
It may be quicker to delete the file and create a new one than to reset it, but doing so opens you up to a race condition that trips up a careless program or makes it vulnerable to an attacker. The attacker could make a link pointing to the file /etc/precious with the same name as your file between the time when you deleted the file and when you recreated it. When the DBM library opens the file, it clobbers /etc/precious.
If you delete a DB_File database and recreate it, you’ll lose any customizable settings like page size, fill-factor, and so on. This is another good reason to assign the empty list to the tied hash.
The documentation for the standard DB_File module, also in Chapter 7
of Programming Perl; the
unlink function in
(1); Section 14.1