my $sql = "SELECT ID FROM User WHERE Status = 'X';"; my $sth = $dbh->prepare($sql); $sth->execute; my $cnt = 0; while ( my $id = $sth->fetchrow_array ) { if (-e "bilder\\$id.jpg") { system("del /F /Q bilder\\$id.jpg"); } $cnt++; } $dbh->do("DELETE FROM User WHERE Status = 'X';");