Recorder.purge not working

I have this in my configuration.yaml

recorder:
  purge_keep_days: 2
  purge_interval: 1
  exclude: 
    entities:
      - binary_sensor.family_room_motion_detector
      - binary_sensor.hallway_motion_detector

home-assistant_v2.db continues to grow and it is not being purged, the exclude entities is working fine

Any ideas

1 Like

Have you been restarting every day?

Restarting home assistant resets the purge interval timer.

1 Like

Yes, I have restarting often as I have been working on my Home Assistant a bunch the last few days. What you said makes sense, why didnā€™t I think about that.

thanks for your help

Just throwing it out thereā€¦

I donā€™t believe it as my experience corresponds with yours but there seems to be a PR thatā€™s going to correct this soon.

Iā€™m still confused after reading that thread. Is it true or a rumour?

The code tries to get back into sync after a restart. So if it purged last night at 9PM and you restart today at 6PM, it should purge in about 3 hoursā€™ time (with a lower limit of 30 minutes).

So I guess the fully correct way to state it is that it can take up to 24 hours.

Iā€™m confused as well however I believe what we both said is correctā€¦ is if you restart HA it resets.

Hi,
i dont use this because its ā€œglobalā€.
ā€œsomeā€ of my sensordata should be purged the next day some of them should keeped over a longer period.

to add this you need sqlite3 and a external script - running as cronjob on your system.

eg: i have a sensor ā€œnext holidays (next_free)ā€ or ā€œnext garbage date & type (sensor.fes)ā€

Itā€™s nice to knew the actual state but the state from yesterday is just garbage.

Here is a cutout from my script - save it somewhere, make it executable
check if your database is in the same path as on my setup
(/usr/share/hassio/homeassistant/home-assistant_v2.db)

all the ā€œmagicā€ happens here:
drop everything from sensor.fes older than 1 day:
statement=ā€œ$older_states_than 1 daysā€™) AND entity_id LIKE ā€˜%sensor.fes%ā€™ā€

and run the script \edit with sudo./edit

When the output looks good for you - change remove the # in front of the sqlite3 and add them infront of the echo lines.

#!/bin/bash

#var
db=/usr/share/hassio/homeassistant/home-assistant_v2.db
older_events_than="time_fired < datetime('now','-"
older_states_than="created < datetime('now','-"

#stop HA
docker stop homeassistant

#backup db
cp $db ~/home-assistant_v2.db

events() {
         echo sqlite3 $db "SELECT * FROM events WHERE $statement;"
         #sqlite3 $db "DELETE FROM events WHERE $statement;"
         }

states() {
         echo sqlite3 $db "SELECT * FROM states WHERE $statement;"
         #sqlite3 $db "DELETE FROM states WHERE $statement;"
         }

#events
statement="$older_events_than 1 days') AND event_data LIKE '%%next_free%%'"
events

statement="$older_events_than 1 days') AND event_data LIKE '%sensor.fes%'"
events

#states
statement="$older_states_than 1 days') AND entity_id LIKE '%sensor.fes%'"
states

#rebuild db
sqlite3 $db "vacuum;"
#start HA
docker start homeassistant

cheers

I dug into the history. It seems that it used to be true, so not a rumor. However, it is no longer true since November 2017 where PR #10279 was merged.

1 Like

This is true. I notice the created, changed and last opened time stamps on the db file ate all identical to the time I executed the recorder.purge command.

What if some script back-dated the file date of the db file everytime HA starts?
ā€¦ or is the purge interval timer a separate entity within HA?

As of version 0.109, the default is for purge to run at 04:12 every morning, whether you have restarted or not.

Recorder - The recorder configuration purge_interval has been deprecated. The recorder database is now purged every night. This can be disabled with the new auto_purge configuration though that is usually not recommended. - (@amelchio - #33646) (recorder docs)

1 Like

Hi everyone!

Iā€™m having issues with the purge settings. I have been deleting the file manually because the auto purge nor a automation works.

I have the following configuration within configuration.yaml but it doesnā€™t erase anything.

recorder:
  auto_purge: true

Iā€™ve also tried to set an automation or even run the service recorder.purge right away but the data is still there.

Any idea of what this can be?

Iā€™m using the default db.

Edit: Iā€™m using the version 0.112.2

Thanks!

If that is the only config you have, then you are using the default purge_keep_days of 10.

So it wonā€™t begin purging anything until 10 days after you added the recorder (I.e. the data is 10 days old).

Thank for your input!

Iā€™ve added the purge_keep_days, restarted home assistant and runned the service again, but no luckā€¦ The history from the last days is still there :frowning_face:

recorder:
    auto_purge: true
    purge_keep_days: 1

The auto_purge shoudnā€™t auto delete everything at 4AM?

Thanks!

it will autodelete everything older than 1 day. The database size wonā€™t change with those settings though. To make it run now, you can call the service from dev tools.

1 Like

And to actually delete the rather than just mark as ā€œdeletedā€ you have to repack.

Read this: https://www.home-assistant.io/integrations/recorder/#service-purge

1 Like