coollabsio/coolify · error · RuntimeException

This source is being used by an application. Please delete a

Error message

This source is being used by an application. Please delete all applications first.

What it means

Thrown from a deleting hook in GitlabApp::booted() (app/Models/GitlabApp.php:101). Coolify blocks deletion of a GitLab source while the morph-count of applications attached via $gitlabApp->applications() is greater than zero, preventing orphaned applications whose deployments depend on that GitLab connection. It is the GitLab equivalent of the GitHub App delete guard, raised as a RuntimeException instead of \Exception.

Source

Thrown at app/Models/GitlabApp.php:101

    }

    public static function findByWebhookToken(string $token): ?self
    {
        if ($token === '') {
            return null;
        }

        // Encrypted values cannot be matched with a SQL equality; sources are few per instance.
        return static::query()->get()->first(
            fn (self $app): bool => filled($app->webhook_token) && hash_equals((string) $app->webhook_token, $token)
        );
    }

    protected static function booted(): void
    {
        static::deleting(function (GitlabApp $gitlabApp) {
            if ($gitlabApp->applications()->count() > 0) {
                throw new \RuntimeException('This source is being used by an application. Please delete all applications first.');
            }
        });
    }

    public static function ownedByCurrentTeam()
    {
        return GitlabApp::where(function ($query) {
            $query->where('team_id', currentTeam()->id)
                ->orWhere('is_system_wide', true);
        });
    }

    public static function public()
    {
        return GitlabApp::where(function ($query) {
            $query->where('team_id', currentTeam()->id)->orWhere('is_system_wide', true);
        })->where('is_public', true);
    }

View on GitHub (pinned to 70b9acc424)

Solutions

  1. List the dependents first: $gitlabApp->applications()->pluck('name') — delete those applications or re-point them to another GitLab source.
  2. Retry the source deletion after the applications() count reaches zero.
  3. For scripts, wrap deletion and skip sources that report dependents instead of failing the whole run.

Example fix

// before
$gitlabApp->delete(); // RuntimeException while apps remain

// after
if ($gitlabApp->applications()->exists()) {
    // migrate or delete dependent apps first
    return 'Source in use by: '.$gitlabApp->applications()->pluck('name')->implode(', ');
}
$gitlabApp->delete();
Defensive patterns

Strategy: validation

Validate before calling

// Before deleting a GitLab source, check its applications
if ($gitlabApp->applications()->exists()) {
    // handle each application (delete or change source), then retry
}

Try / catch

try {
    $gitlabApp->delete();
} catch (\RuntimeException $e) {
    if (str_contains($e->getMessage(), 'being used by an application')) {
        return 'Delete the applications attached to this GitLab source first.';
    }
    throw $e;
}

Prevention

When it happens

Trigger: Calling $gitlabApp->delete() (UI 'Delete' on a GitLab source) while at least one Application is related through the applications() relation. Bulk-deleting sources in a script without first detaching or removing dependent applications.

Common situations: Rotating GitLab instances (self-hosted → gitlab.com) and deleting the old source while projects still reference it; cleaning up 'unused' sources after a team audit without checking cross-project apps; scripts that wipe all GitlabApp rows for a team.

Related errors


AI-assisted analysis of coollabsio/coolify@70b9acc424 (2026-08-17). Data as JSON: /api/errors/44da9e8e5f37f5f8. Report an issue: GitHub.