fshr
The musings of a grumpy hairless ape
Migrating InfluxDB 2 Data
I recently had a need to move some data between InfluxDB 2.x buckets (I’d previously lumped different pieces of data in the same bucket and wanted to separate them).
Unfortunately InfluxDB doesn’t have a built-in method to simply move data, so you need to get a bit more creative.
Using the InfluxDB 2.x Data Explorer, the following will copy measurements from one bucket to the other (note it also renames the measurement to something more useful than “mqtt_consumer”):
from(bucket: "telegraf")
|> range(start:-1y)
|> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
|> filter(fn: (r) => r["topic"] == "zigbee2mqtt/Sensor/Study-TH")
|> set(key: "_measurement", value: "home_mqtt")
|> to(bucket: "home_data", org: "pfish")
Once the data’s copied over, telegraf is updated to send it’s data to the new bucket and measurement, and (in my case) Grafana is updated to point to the new bucket, the old data can be deleted:
curl --request POST "http://<<IP>>:8086/api/v2/delete?org=pfish&bucket=telegraf" \
--header 'Authorization: Token <<API-TOKEN>>' \
--header 'Content-Type: application/json' \
--data '{
"start": "1970-01-01T00:00:00Z",
"stop": "2025-12-31T23:59:00Z",
"predicate": "_measurement=\"mqtt_consumer\""
}'
Unfortunately, the delete only removes the datapoints, and doesn’t remove the measurement schema, but I can live with that for now (until I work out how to remove it!)