As long as there are no circular references, you can find the order by trying. By following the next approach you'll set up several (overlapping) tree structures, which you can then delete from root to leaf.
If a table can be cleared (DELETE FROM tablename), then it is a root node. If it is not,
you should be able to read from the error which table(s) depend(s) on that table, and those tables will be the parents of the current table. You'll just continue until a table can be deleted without problems.
You could automatically build these trees, if you can determine from the error messages which table you should try to delete next.
Alternatively, you can store all tables in a collection, and keep trying to clear one at a time, removing all successfully cleared tables, until either nothing can be removed (circular references) or the collection is empty: