Prolog destructor?

I have an init predicate which asserts some facts. I would like to clean these up after execution.

I'm running queries of the form init, do_stuff, cleanup.. I would rather not have a separate cleanup call if i can avoid it (because I forget to call it).

I found this as a solution:

init:-
assert(foo); not(retract(foo)).


Are there cases I would not want to do this or reasons why I shouldn't?

• Just warning you prolog is not very active on this site. Only four questions on it were asked since January... – fartgeek Nov 6 '20 at 21:02
• Wonder whether we can add it to the iftt bot that posts to Twitter. I will accept comments years from now as well :) – 2bigpigs Nov 7 '20 at 16:51
• Why not use setup_call_cleanup and create a meta predicate? run_query(Init_fact, Goal) :- setup_call_cleanup(assert(Init_fact), Goal, retract(Init_fact)). – rajashekar Nov 8 '20 at 2:11