Is there a suggested approach for deleting Grafana dashboards older than X days? The dashboards are growing quite quickly in our MySQL DB, making the SELECT statements when searching very slow.
I’m not sure what you mean by “dashboards older than X days”.
What is creating new dashboards and putting them into MySQL?
We have API’s that programmatically create Grafana Dashboards at scale. Each dashboard is typically only needed for a few weeks. After that, we would like to delete them.
So, why not use those same APIs to delete them when no longer needed?
I don’t understand the problem.
The Dashboard API doesn’t have any field in its response that tells you when it was created.
Here’s the north star I’m envisioning - I could run DELETE * from Dashboards where created_on < X DAYs ago. Somehow it doesn’t seem like this is the best approach
There is a dashboard versions API. Created date of version 1 is a date when dashboard was created. It will work if you don’t have many dashboards updates. Just add a few more API calls.
The Dashboard API doesn’t have any field in its response that tells you
when it was created.
Maybe (quite probably) I am ignorant regarding the creation of dashboards in
MySQL, but is it not possible to add a field for the creation time, so that
your SQL statement
DELETE * from Dashboards where created_on < X DAYs ago
can simply work as required?
I am not familiar with dashboards being defined in MySQL (but I must admit I
find it an intriguing opportunity); please can you please point me at the
documentation for how that works?
@pooh I guess that what @tngtwitter is referring to with MySQL is the database that Grafana uses to store its own metadata (incl. dashboards)
@tngtwitter if you do use a MySQL DB as your Grafana store, have you tried running something like: ?
DELETE FROM dashboard WHERE created < ...;
Looking at the Postgres schema for the Grafana DB (and assuming it’s the same for MySQL), the table name
dashboard and the column name
created do exist, so this could work. Or you may get a foreign key error, since there’s a likelihood that other tables will refer to the dashboards that you’re trying to delete - though that just means that you’ll need a more complex query which also deletes these.
All in all, though, the API approach that @jangaraj recommends is probably better than direct database manimulation.