I'm clocking the time spent on daily tasks like email and for that I
have a "Daily Routine" TODO entry. Clocking in this task adds a new
clock line everyday, leading to a very populated CLOCK drawer. I'd like
to limit its size by either a maximum number of clock entries by
removing the older ones or better, removing the entries older than some
date. Ideally, I'd like to find a function that tidies the clock drawer of an entry, to be added to the clock-in or clock-out hook.
Have a look at org-clock-remove-empty-clock-drawer. It uses org-map-tree and a regular expression based on org-clock-drawer-name to find the :LOGBOOK: drawers.
You can use the same logic to get point to the start of :LOGBOOK: and then handle all CLOCK: entries on a line-by-line basis. To parse the timestamps, you can use org-ts-regexp-both and org-element-timestamp-parser. Afterwards, you can use kill-whole-line to get rid of the entry.
So in short:
Find the next :LOGBOOK: via re-search-forward
Go forward a single line
Check the start of the line.
If it starts with :END:, stop the clock handling for the current drawer
If it starts with CLOCK:, search for the next timestamp.
If it's too old, remove the whole line with kill-whole-line.