Delete all nodes where the taxonomy term matches a given value I have a content type (example_content_type) with 3 3 fields: title, body, and pks_type. pks_type is a taxonomy term select list field. and 'pks_type'has 2 terms:


Type1, tid = 1
Type2, tid = 2


I have 150 nodes using those taxonomy terms. Now I need to delete all nodes from 'example_content_type' except last 5 nodes, where the taxonomy term ID is 1.

How can I achieve this with db_delete()?
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.