Repository: appwrite/appwrite
File: src/Appwrite/Platform/Workers/Deletes.php (PHP)

Code before:
     */
    private function deleteUsageStats(Document $project, callable $getProjectDB, callable $getLogsDB, string $hourlyUsageRetentionDatetime): void
    {
        /** @var Database $dbForProject*/
        $dbForProject = $getProjectDB($project);

        // Delete Usage stats from projectDB
        $this->deleteByGroup('stats', [
            Query::select(['$internalId', '$id', '$updatedAt']),
            Query::equal('period', ['1h']),
            Query::lessThan('time', $hourlyUsageRetentionDatetime),
            Query::orderDesc('time'),
            Query::orderDesc(), // _key_period_time `period` ASC, `time `ASC
        ], $dbForProject);

        if ($project->getId() !== 'console') {
            /** @var Database $dbForLogs*/
            $dbForLogs = call_user_func($getLogsDB, $project);

            // Delete Usage stats from logsDB
            $this->deleteByGroup('stats', [
                Query::select(['$internalId', '$id', '$updatedAt']),
                Query::equal('period', ['1h']),
                Query::lessThan('time', $hourlyUsageRetentionDatetime),
                Query::orderDesc('time'),
                Query::orderDesc(), // _key_period_time `period` ASC, `time `ASC
            ], $dbForLogs);
        }
    }

    /**
     * @param callable $getProjectDB
     * @param Document $document teams document
     * @param Document $project
     * @return void
     * @throws Exception
     */
    public function deleteMemberships(callable $getProjectDB, Document $document, Document $project): void
    {
        $dbForProject = $getProjectDB($project);
        $teamInternalId = $document->getInternalId();

        // Delete Memberships
        $this->deleteByGroup(
            'memberships',
            [
                Query::equal('teamInternalId', [$teamInternalId]),
                Query::orderAsc()
            ],
            $dbForProject,
            function (Document $membership) use ($dbForProject) {

Review comment (suggestion):
```suggestion
                Query::orderDesc(),
```

Code after:
            Query::lessThan('time', $hourlyUsageRetentionDatetime),
            Query::orderDesc('time'),
            Query::orderDesc(),
        ], $dbForProject);

        if ($project->getId() !== 'console') {
            /** @var Database $dbForLogs*/
            $dbForLogs = call_user_func($getLogsDB, $project);

            // Delete Usage stats from logsDB
            $this->deleteByGroup('stats', [
                Query::select(['$internalId', '$id', '$updatedAt']),
                Query::equal('period', ['1h']),
                Query::lessThan('time', $hourlyUsageRetentionDatetime),
                Query::orderDesc('time'),
                Query::orderDesc(),
            ], $dbForLogs);
        }
    }

    /**
     * @param callable $getProjectDB
     * @param Document $document teams document
     * @param Document $project
     * @return void
     * @throws Exception
     */
    public function deleteMemberships(callable $getProjectDB, Document $document, Document $project): void
    {
        $dbForProject = $getProjectDB($project);
        $teamInternalId = $document->getInternalId();

        // Delete Memberships
        $this->deleteByGroup(
            'memberships',
            [
                Query::equal('teamInternalId', [$teamInternalId]),
                Query::orderAsc()
            ],
            $dbForProject,
            function (Document $membership) use ($dbForProject) {
                $userId = $membership->getAttribute('userId');
                $dbForProject->purgeCachedDocument('users', $userId);
            }
        );
    }

    /**
     * @param Database $dbForPlatform
     * @param Document $document
     * @return void