$query = "delete from table where id=("; for(@MESSAGE_IDS){ # you may need a "chomp" in here $query.=$_." or id="; } #Delete "or" off the back $query=substr($query,0,-7)); #close the query $query.=")"; my $sth= $dbh->prepare($query); $sth->execute;