How to move data from one measurement to another

Hi there, I’m pretty new to flux language and currently trying to figure out how to move measurement data from one measurement to another.

I stored data under a wrong measurement name. I corrected my data source, now I want to move the old data to the correct measurement. With InfluxQL I used SELECT INTO as outlined in this SE discussion.

I guess I need the to() function, but so far I was not successful… How do I specify the destination measurement name?

Hello @moritzj29,
Welcome! Are you using InfluxDB 1.x or 2.x?
What did you try already? Can you please share some Flux code?
The to() function converts output data into line protocol and writes it to InfluxDB. Line protocol requires each record to have a timestamp, a measurement, a field, and a value. All output data must include the following columns:

  • _time
  • _measurement
  • _field
  • _value

So what you must do is use the map() function to rename the value of your measurement column to write data to a new measurement.

map(fn: (r) => ({ _measurement: "new_measurement" }))
1 Like

Hello @Anaisdg,
thanks a lot for your reply, it was a great help! I was missing out on the map() part completely, so no wonder it didn’t work.
Here is the code I ended up using, maybe it is helpful for somebody else:

from(bucket: "my_db")
|> range(start:-1y)
|> filter(fn: (r) => r["_measurement"] == "old_measurement")
|> map(fn: (r) => ({ r with _measurement: "new_measurement" }))
|> to(bucket: "my_db", org: "myOrg")

I used with in the map() to keep all other tags etc.

Unfortunately, for deletion of measurements one has to go to command line or REST API, using the nice webUI is not possible. I went for the HTTP request:

curl -iL --request POST '' \
--header 'Authorization: Token YOUR_API_TOKEN' \
--header 'Content-Type: application/json' \
--data '{
	"start": "2021-01-01T00:00:00Z",
	"stop": "2021-09-08T00:00:00Z",
	"predicate": "_measurement=\"old_measurement\""

I used the -L flag since I run InfluxDB behind a reverse proxy in a local environment and therefore the request is redirected. Omitting the flag results in 308 responses and nothing happens. -i is helpful to print the answer, also -v for very detailed output may be helpful for debugging.

Quite some journey for a couple of wrongly named measurements… Hope this post saves someone else some time.