'Clear expired data from the backend storage system.
Clearing occurs according to the time-to-live.
:param ttl: Number of seconds to keep records for.'

def clear_expired_metering_data(self, ttl):

    session = self._engine_facade.get_session()
    with session.begin():
        end = (timeutils.utcnow() - datetime.timedelta(seconds=ttl))
        sample_q = session.query(models.Sample).filter((models.Sample.timestamp < end))
        rows = sample_q.delete()
        LOG.info('%d    samples    removed    from    database', rows)
    if (not self.conf.database.sql_expire_samples_only):
        with session.begin():
            session.query(models.Meter).filter((~ models.Meter.samples.any())).delete(synchronize_session=False)
        with session.begin():
            resource_q = session.query(models.Resource.internal_id).filter((~ models.Resource.samples.any()))
            resource_q.update({models.Resource.metadata_hash: ('delete_' + cast(models.Resource.internal_id, sa.String))}, synchronize_session=False)
        for table in [models.MetaText, models.MetaBigInt, models.MetaFloat, models.MetaBool]:
            with session.begin():
                resource_q = session.query(models.Resource.internal_id).filter(models.Resource.metadata_hash.like('delete_%'))
                resource_subq = resource_q.subquery()
                session.query(table).filter(table.id.in_(resource_subq)).delete(synchronize_session=False)
        with session.begin():
            resource_q = session.query(models.Resource.internal_id).filter(models.Resource.metadata_hash.like('delete_%'))
            resource_q.delete(synchronize_session=False)
        LOG.info('Expired    residual    resource    and    meter    definition    data')
