use Data::Dumper; my $family={ father=>'foo', mother=>'bar',}; for(keys %$family){ if(!defined($_)){ delete $family->{$_} } } print Dumper $family; # print out your hash #### use Data::Dumper; my $family={ father=>'foo', mother=>'bar',}; for(keys %$family){ if(defined($_)){ delete $family->{$_} } } print Dumper $family; # print empty hash #### my $file = sprintf('%s/%s/%02x.db', $qdir, $mypc, $i);