Delete all nodes where the taxonomy term matches a given value
The major problem here is the "except last 5" constraint. I suggest the following solution:

db_select the nodes' nids that you don't want to delete in the first step, like

$savenids = select nid from node where -whatever- limit 5.

Then db_delete the rest of the nodes like 

delete from node where -whatever- and nid not in $savenids.

The -whatever- terms strongly depend on your field setup, take a look at your tables for that.