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

Code before:
        $dbForProject = $getProjectDB($project);
        $duration = $project->getAttribute('auths', [])['duration'] ?? Auth::TOKEN_EXPIRATION_LOGIN_LONG;
        $expired = DateTime::addSeconds(new \DateTime(), -1 * $duration);

        // Delete Sessions
        $this->deleteByGroup('sessions', [
            Query::select(['$internalId', '$id', '$updatedAt']),
            Query::lessThan('$createdAt', $expired),
            Query::orderDesc('$createdAt'),
            Query::orderDesc(),
        ], $dbForProject);
    }

    /**
     * @param Database $dbForPlatform
     * @param string $datetime
     * @return void
     * @throws Exception
     */
    private function deleteRealtimeUsage(Database $dbForPlatform, string $datetime): void
    {
        // Delete Dead Realtime Logs
        $this->deleteByGroup('realtime', [
            Query::lessThan('timestamp', $datetime),
            Query::orderDesc('timestamp'),
            Query::orderAsc(), // KEY "_key_timestamp" ("timestamp" DESC),
        ], $dbForPlatform);
    }

    /**
     * @param Database $dbForPlatform
     * @param callable $getProjectDB
     * @param string $auditRetention
     * @return void
     * @throws Exception
     */
    private function deleteAuditLogs(Document $project, callable $getProjectDB, string $auditRetention): void
    {
        $projectId = $project->getId();
        $dbForProject = $getProjectDB($project);

        try {
            $this->deleteByGroup(Audit::COLLECTION, [
                Query::select(['$internalId', '$id', '$updatedAt']),
                Query::lessThan('time', $auditRetention),
                Query::orderDesc('time'),
                Query::orderAsc(), // KEY "index-time" ("time" DESC)
            ], $dbForProject);
        } catch (DatabaseException $e) {
            Console::error('Failed to delete audit logs for project ' . $projectId . ': ' . $e->getMessage());
        }

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

Code after:
     * @throws Exception
     */
    private function deleteRealtimeUsage(Database $dbForPlatform, string $datetime): void
    {
        // Delete Dead Realtime Logs
        $this->deleteByGroup('realtime', [
            Query::lessThan('timestamp', $datetime),
            Query::orderDesc('timestamp'),
            Query::orderAsc(),
        ], $dbForPlatform);
    }

    /**
     * @param Database $dbForPlatform
     * @param callable $getProjectDB
     * @param string $auditRetention
     * @return void
     * @throws Exception
     */
    private function deleteAuditLogs(Document $project, callable $getProjectDB, string $auditRetention): void
    {
        $projectId = $project->getId();
        $dbForProject = $getProjectDB($project);

        try {
            $this->deleteByGroup(Audit::COLLECTION, [
                Query::select(['$internalId', '$id', '$updatedAt']),
                Query::lessThan('time', $auditRetention),
                Query::orderDesc('time'),
                Query::orderAsc(),
            ], $dbForProject);
        } catch (DatabaseException $e) {
            Console::error('Failed to delete audit logs for project ' . $projectId . ': ' . $e->getMessage());
        }
    }

    /**
     * @param callable $getProjectDB
     * @param Device $deviceForFunctions
     * @param Device $deviceForBuilds
     * @param Document $document function document
     * @param Document $project
     * @return void
     * @throws Exception
     */
    private function deleteFunction(Database $dbForPlatform, callable $getProjectDB, Device $deviceForFunctions, Device $deviceForBuilds, CertificatesAdapter $certificates, Document $document, Document $project): void
    {
        $projectId = $project->getId();
        $dbForProject = $getProjectDB($project);
        $functionId = $document->getId();
        $functionInternalId = $document->getInternalId();