... while (my $row = $sth->fetchrow_hashref()) { my $del_1 = $row->{image_1} || ''; my $del_2 = $row->{image_2} || ''; my $del_3 = $row->{image_3} || ''; my $del_4 = $row->{image_4} || ''; } my $dir_to_open = "../../pics"; opendir(DIR,$dir_to_open) || die("Cannot open directory !\n"); @dir_contents= readdir(DIR); closedir(DIR); foreach $file (@dir_contents){ if(!(($file eq ".") || ($file eq ".."))){ # I want to delete the files found in here if they match what was found above in the database call } }