async fn prune_storage_logs(
        &mut self,
        l2_blocks_to_prune: ops::RangeInclusive<L2BlockNumber>,
    ) -> DalResult<u64> {
        // Storage log pruning is designed to use deterministic indexes and thus have predictable performance.
        //
        // - The WITH query is guaranteed to use the block number index (that's the only WHERE condition),
        //   and the supplied range of blocks should be reasonably small.
        // - The main DELETE query is virtually guaranteed to use the primary key index since it removes ranges w.r.t. this index.
        //
        // Using more sophisticated queries leads to fluctuating performance due to unpredictable indexes being used.
        let execution_result = sqlx::query!(
            r#"
            WITH
            new_logs AS MATERIALIZED (
                SELECT DISTINCT
                ON (hashed_key)
                    hashed_key,
                    miniblock_number,
                    operation_number
                FROM
                    storage_logs
                WHERE
                    miniblock_number BETWEEN $1 AND $2
                ORDER BY
                    hashed_key,
                    miniblock_number DESC,
                    operation_number DESC
            )
            
            DELETE FROM storage_logs USING new_logs
            WHERE
                storage_logs.hashed_key = new_logs.hashed_key
                AND storage_logs.miniblock_number <= $2
                AND (storage_logs.miniblock_number, storage_logs.operation_number)
                < (new_logs.miniblock_number, new_logs.operation_number)
            "#,
            i64::from(l2_blocks_to_prune.start().0),
            i64::from(l2_blocks_to_prune.end().0)
        )
        .instrument("hard_prune_batches_range#prune_storage_logs")
        .with_arg("l2_blocks_to_prune", &l2_blocks_to_prune)
        .report_latency()
        .execute(self.storage)
        .await?;
        Ok(execution_result.rows_affected())
    }
Removes storage logs overwritten by the specified new logs.
core/lib/dal/src/pruning_dal/mod.rs