Averaging measurement series with influx task

I have an influx bucket with the sensor data from temperature sensors (via iobroker).


Over the time it grew quite a lot, now /var/lib/influxdb/engine/data is over 40GB big. So I want to reduce the saved measurements, and only keep the average from 5 hours, if it is older than 90 days.

I got a working query to show the graph, but I don’t find a good solution, how to replace the old data with the averages.

from(bucket: "iobroker")
  |> range(start: -90d)
  |> filter(fn: (r) => r["_measurement"] == "zigbee.0.a4c13880c49341d8.temperature")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: 5h, fn: mean, createEmpty: false)
  |> yield(name: "mean")

It would be great if you could give me a hint how I could replace now the old data. Maybe there is an easier solution, I’m not aware of.
Thanks!