You will need a custom module which will track term usage.
file my_module.install
/**
* Implements hook_install()
*/
function my_module_install() {
$spec = array(
'description' => 'Last used timestamp',
'type' => 'int',
'not null' => TRUE,
'default' => 0
);
db_add_field('taxonomy_index', 'used', $spec);
}
return true;
}
file `my_module.module`
function my_module_node_update($node) {
//read all fields created by taxonomy module
$term_fields = array_intersect_key(field_info_instances('node', $node->type), field_read_fields(array('module' => 'taxonomy')));
// array for storing added terms
$diff_terms = array();
foreach ($term_fields as $field_name => $field) {
$new_terms = $old_terms = array();
// detect language
$field_langcode = field_language('node', $node, $field_name, LANGUAGE_NONE);
// we need onli "tid" field
foreach ($node->{$field_name}[$field_langcode] as $term) {
$new_terms[] = $term['tid'];
}
foreach ($node->original->{$field_name}[$field_langcode] as $term) {
$old_terms[] = $term['tid'];
}
$diff_terms += array_diff($new_terms, $old_terms);
}
// update table - set current timestamp
db_update('taxonomy_index')
->fields(array('used' => time(),))
->condition('taxonomy_index.tid', $diff_terms, 'IN')
->execute();
}
This will update all terms, used by nodes. In table "taxonomy_index" you'll get new field with last updated timestamp
After you can use hook_cron to delete "old" terms.