Deleting a Time Series
Video Lecture
Description
There may be a time when you want to delete data from the Prometheus TSDB database.
Data will be automatically deleted after the storage retention time has passed. By default, it is 15 days.
If you want to delete specific data earlier, then you are able.
You need to enable the admin API in Prometheus before you can.
sudo nano /etc/default/prometheus
Add --web.enable-admin-api
to the ARGS=""
variable. E.g.,
ARGS="--web.enable-admin-api"
Restart Prometheus and check status
sudo service prometheus restart
sudo service prometheus status
You can now make calls to the admin API.
In my example I want to delete all time series for the instance="sbcode.net:9100"
So I run the delete_series API endpoint providing the value to match. E.g.,
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={instance="sbcode.net:9100"}'
When I re-execute the Prometheus query, the time series I wanted to be deleted no longer exists.
You can have more complicated match queries, e.g.,
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=a_bad_metric&match[]={region="mistake"}'
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=node_exporter:memory:percent'
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=node_filesystem_free_percent'
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=node_memory_MemFree_percent'
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=ALERTS'
curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=ALERTS_FOR_STATE'
After deleting you should disable the admin API again,
sudo nano /etc/default/prometheus
Remove --web.enable-admin-api
from the ARGS
variable. E.g.,
ARGS=""
Restart Prometheus and check status
sudo service prometheus restart
sudo service prometheus status
Useful Links
Prometheus 2.31 and Ubuntu 22.04 Notes
In the video, I demonstrate the --storage.tsdb.retention
flag. This flag has been deprecated. Now use --storage.tsdb.retention.time
instead.
E.g.,
ARGS="--storage.tsdb.retention.time=1y"
The default value remains 15d
. Units Supported: y, w, d, h, m, s, ms.