How to delete all parent references of a taxonomy term? How to delete references to parent terms?

I tried this:

$term-&gt;get('parent')-&gt;setValue(NULL);
$term-&gt;save();


Additional question: how to delete reference to a specific parent term?
Answering to myself. Taxonomy hierarchy is known to be detached from terms. For example when loading term you don't get its parents. This was confusing to me and I thought setValue() just doesn't work. But the truth is that it works perfectly.

To get the code from above working, only a minor edit is required - it's using 0, not NULL:

$term-&gt;get('parent')-&gt;setValue(0);


This deletes all the hierarchy the term has and writes one record to the taxonomy_term_hierarchy table - with zero parent.

If you need to delete the hierarchy manually you can use deleteTermHierarchy():

\Drupal::entityTypeManager()-&gt;getStorage('taxonomy_term');-&gt;deleteTermHierarchy($tid);


but be warned that the term will disappear from the lists on /admin/structure/taxonomy/manage/&lt;vocabulary&gt;/overview page since it's missing any hierarchy now. To fix this you need to add a record to the taxonomy_term_hierarchy table with zero parent. And that's exactly what setValue(0) does - deletes hierarchy and adds that record.