GitLab Rails Console Cheat Sheet (FREE SELF)

This is the GitLab Support Team's collection of information regarding the GitLab Rails console, for use while troubleshooting. It is listed here for transparency, and for users with experience with these tools. If you are currently having an issue with GitLab, it is highly recommended that you first check our guide on our Rails console, and your support options, before attempting to use this information.

WARNING: Some of these scripts could be damaging if not run correctly, or under the right conditions. We highly recommend running them under the guidance of a Support Engineer, or running them in a test environment with a backup of the instance ready to be restored, just in case.

WARNING: As GitLab changes, changes to the code are inevitable, and so some scripts may not work as they once used to. These are not kept up-to-date as these scripts/commands were added as they were found/needed. As mentioned above, we recommend running these scripts under the supervision of a Support Engineer, who can also verify that they continue to work as they should and, if needed, update the script for the latest version of GitLab.


View available attributes, formatted using pretty print (pp).

For example, determine what attributes contain users' names and email addresses:

u = User.find_by_username('someuser')
pp u.attributes

Partial output:

 "name"=>"S User",

Then make use of the attributes, testing SMTP, for example:

e =
n =
Notify.test_email(e, "Test email for #{n}", 'Test email').deliver_now
Notify.test_email(, "Test email for #{}", 'Test email').deliver_now

Imports and exports

Import a project

# Find the project and get the error
p = Project.find_by_full_path('<username-or-group>/<project-name>')


# To finish the import on GitLab running version before 11.6

# To finish the import on GitLab running version 11.6 or after
p.import_state.mark_as_failed("Failed manually through console.")

Rename imported repository

In a specific situation, an imported repository needed to be renamed. The Support Team was informed of a backup restore that failed on a single repository, which created the project with an empty repository. The project was successfully restored to a development instance, then exported, and imported into a new project under a different name.

The Support Team was able to transfer the incorrectly named imported project into the correctly named empty project using the steps below.

Move the new repository to the empty repository:

mv /var/opt/gitlab/git-data/repositories/<group>/<new-project> /var/opt/gitlab/git-data/repositories/<group>/<empty-project>

Make sure the permissions are correct:

chown -R git:git <path-to-directory>.git

Clear the cache:

sudo gitlab-rake cache:clear

Export a project

It's typically recommended to export a project through the web interface or through the API. In situations where this is not working as expected, it may be preferable to export a project directly via the Rails console:

user = User.find_by_username('<username>')
# Sufficient permissions needed
# Read

project = Project.find_by_full_path('<username-or-group>/<project-name'), user).execute

If this all runs successfully, you see an output like the following before being returned to the Rails console prompt:

=> nil

The exported project is located in a .tar.gz file in /var/opt/gitlab/gitlab-rails/uploads/-/system/import_export_upload/export_file/.

If this fails, enable verbose logging, repeat the above procedure after, and report the output to GitLab Support.


Find mirrors with "bad decrypt" errors

This content has been converted to a Rake task, see verify database values can be decrypted using the current secrets.

Transfer mirror users and tokens to a single service account

This content has been moved to Troubleshooting Repository mirroring.


Create new user

u = 'test_user', email: '', name: 'Test User', password: 'password', password_confirmation: 'password')
u.skip_confirmation! # Use it only if you wish user to be automatically confirmed. If skipped, user receives confirmation e-mail!

Skip reconfirmation

user = User.find_by_username('<username>')

Disable 2fa for single user

In GitLab 13.5 and later:

Use the code under Disable 2FA | For a single user so that the target user is notified that 2FA has been disabled.

In GitLab 13.4 and earlier:

user = User.find_by_username('<username>')

Active users & Historical users

# Active users on the instance, now

# Users taking a seat on the instance

# The historical max on the instance as of the past year
::HistoricalData.max_historical_user_count(from: 1.year.ago.beginning_of_day, to: Time.current.end_of_day)

Using cURL and jq (up to a max 100, see Pagination):

curl --silent --header "Private-Token: ********************" \
     "" | jq --compact-output '.[] | [.id,.name,.username]'

Update Daily Billable & Historical users

# Forces recount of historical (max) users

# Forces recount of daily billable users
identifier = Analytics::UsageTrends::Measurement.identifiers[:billable_users], User.minimum(:id), User.maximum(:id),

Block or Delete Users that have no projects or groups

users = User.where('id NOT IN (select distinct(user_id) from project_authorizations)')

# How many users are removed?

# If that count looks sane:

# You can either block the users:
users.each { |user|  user.blocked? ? nil  : user.block! }

# Or you can delete them:
  # need 'current user' (your user) for auditing purposes
current_user = User.find_by(username: '<your username>')

users.each do |user|

Deactivate Users that have no recent activity

days_inactive = 90
inactive_users ="last_activity_on <= ?", days_inactive.days.ago)

inactive_users.each do |user|
    puts "user '#{user.username}': #{user.last_activity_on}"

Block Users that have no recent activity

days_inactive = 90
inactive_users ="last_activity_on <= ?", days_inactive.days.ago)

inactive_users.each do |user|
    puts "user '#{user.username}': #{user.last_activity_on}"

Find a user's max permissions for project/group

user = User.find_by_username 'username'
project = Project.find_by_full_path 'group/project'
user = User.find_by_username 'username'
group = Group.find_by_full_path 'group'

Merge requests

Close a merge request

u = User.find_by_username('<username>')
p = Project.find_by_full_path('<namespace/project>')
m = p.merge_requests.find_by(iid: <iid>) p, current_user: u).execute(m)

Delete a merge request

u = User.find_by_username('<username>')
p = Project.find_by_full_path('<namespace/project>')
m = p.merge_requests.find_by(iid: <iid>) m.project, current_user: u).execute(m)

Rebase manually

u = User.find_by_username('<username>')
p = Project.find_by_full_path('<namespace/project>')
m = p.merge_requests.find_by(iid: <iid>) m.target_project, current_user: u).execute(m)

Set a merge request as merged

Use when a merge request was accepted and the changes merged into the Git repository, but the merge request still shows as open.

If the changes are not merged yet, this action causes the merge request to incorrectly show merged into <branch-name>.

u = User.find_by_username('<username>')
p = Project.find_by_full_path('<namespace/project>')
m = p.merge_requests.find_by(iid: <iid>) p, current_user: u).execute(m)


Cancel stuck pending pipelines

For more information, see the confidential issue

Ci::Pipeline.where(project_id: 'pending').count
Ci::Pipeline.where(project_id: 'pending').each {|p| p.cancel if p.stuck?}
Ci::Pipeline.where(project_id: 'pending').count

Remove artifacts more than a week old

This section has been moved to the job artifacts troubleshooting documentation.

Find reason failure (for when build trace is empty) (Introduced in 10.3.0)


build = Ci::Build.find(78420)


build.dependencies.each do |d| { puts "status: #{d.status}, finished at: #{d.finished_at},
  completed: #{d.complete?}, artifacts_expired: #{d.artifacts_expired?}, erased: #{d.erased?}" }

Try CI integration

p = Project.find_by_full_path('<project_path>')
m = project.merge_requests.find_by(iid: )

Validate the .gitlab-ci.yml

project = Project.find_by_full_path 'group/project'
content = project.repository.gitlab_ci_yml_for(project.repository.root_ref_sha) project,  current_user: User.first).validate(content)

Disable AutoDevOps on Existing Projects

Project.all.each do |p|

Obtain runners registration token


Seed runners registration token

appSetting = Gitlab::CurrentSettings.current_application_settings

Run pipeline schedules manually

You can run pipeline schedules manually through the Rails console to reveal any errors that are usually not visible.

# schedule_id can be obtained from Edit Pipeline Schedule page
schedule = Ci::PipelineSchedule.find_by(id: <schedule_id>)

# Select the user that you want to run the schedule for
user = User.find_by_username('<username>')

# Run the schedule
ps =, user, ref: schedule.ref).execute!(:schedule, ignore_skip_ci: true, save_on_errors: false, schedule: schedule)


See current license information

# License information (name, company, email address)

# Plan:

# Uploaded:

# Started:

# Expires at:

# Is this a trial license?

# License ID for lookup on CustomersDot

# License data in Base64-encoded ASCII format

Check if a project feature is available on the instance

Features listed in


Check if a project feature is available in a project

Features listed in license.rb.

p = Project.find_by_full_path('<group>/<project>')

Add a license through the console

key = "<key>"
license = key)
License.current # check to make sure it applied

This is needed for example in a known edge-case with expired license and multiple LDAP servers.

Remove licenses

To clean up the License History table:

TYPE = :trial?
# or :expired?!)

# or even License.all.each(&:destroy!)


Registry Disk Space Usage by Project

Find this content in the Container Registry troubleshooting documentation.

Run the Cleanup policy now

Find this content in the Container Registry troubleshooting documentation.


This content has been moved to Troubleshooting Sidekiq.


Get information about LFS objects and associated project

o = LfsObject.find_by(oid: "<oid>")
p = Project.find(LfsObjectsProject.find_by_lfs_object_id(

You can then delete these records from the database with:


You would also want to combine this with deleting the LFS file in the LFS storage area on disk. It remains to be seen exactly how or whether the deletion is useful, however.

Decryption Problems

Bad Decrypt Script (for encrypted variables)

This content has been converted to a Rake task, see verify database values can be decrypted using the current secrets.

As an example of repairing, if ProjectImportData Bad count: is detected and the decision is made to delete the encrypted credentials to allow manual reentry:

  # Find the ids of the corrupt ProjectImportData objects
  total = 0
  bad = []
  ProjectImportData.find_each do |data|
      total += 1
    rescue => e
      bad <<

  puts "Bad count: #{bad.count} / #{total}"

  # See the bad ProjectImportData ids

  # Remove the corrupted credentials
  import_data = ProjectImportData.where(id: bad)
  import_data.each do |data|
    data.update_columns({ encrypted_credentials: nil, encrypted_credentials_iv: nil, encrypted_credentials_salt: nil})

If User OTP Secret Bad count: is detected. For each user listed disable/enable two-factor authentication.

The following script searches in some of the tables for encrypted tokens that are causing decryption errors, and update or reset as needed:

wget -O /tmp/encrypted-tokens.rb
gitlab-rails runner /tmp/encrypted-tokens.rb

Decrypt Script for encrypted tokens

This content has been converted to a Rake task, see verify database values can be decrypted using the current secrets.


Reverify all uploads (or any SSF data type which is verified)

  1. SSH into a GitLab Rails node in the primary Geo site.

  2. Open Rails console.

  3. Mark all uploads as "pending verification":

    Upload.verification_state_table_class.each_batch do |relation|
      relation.update_all(verification_state: 0)
  4. This will cause the primary to start checksumming all Uploads.

  5. When a primary successfully checksums a record, then all secondaries rechecksum as well, and they compare the values.

A similar thing can be done for all Models handled by the Geo Self-Service Framework which have implemented verification:

  • LfsObject
  • MergeRequestDiff
  • Packages::PackageFile
  • Terraform::StateVersion
  • SnippetRepository
  • Ci::PipelineArtifact
  • PagesDeployment
  • Upload
  • Ci::JobArtifact
  • Ci::SecureFile

NOTE: GroupWikiRepository is not in the previous list since verification is not implemented. There is an issue to implement this functionality in the Admin UI.


Moved to Geo replication troubleshooting.

Repository verification failures

Moved to Geo replication troubleshooting.

Resync repositories

Moved to Geo replication troubleshooting.

Blob types

Moved to Geo replication troubleshooting.

Generate Service Ping

The Service Ping Guide in our developer documentation has more information about Service Ping.

Generate or get the cached Service Ping

Gitlab::Usage::ServicePingReport.for(output: :all_metrics_values, cached: true)

Generate a fresh new Service Ping

This also refreshes the cached Service Ping displayed in the Admin Area

Gitlab::Usage::ServicePingReport.for(output: :all_metrics_values)

Generate and print

Generates Service Ping data in JSON format.

rake gitlab:usage_data:generate

Generates Service Ping data in YAML format:

rake gitlab:usage_data:dump_sql_in_yaml

Generate and send Service Ping

Prints the metrics saved in conversational_development_index_metrics.

rake gitlab:usage_data:generate_and_send


Call a GraphQL endpoint through the Rails console:

query = <<~EOQ
query securityGetProjects($search: String!) {
  projects(search: $search) {
    nodes {

variables = { "search": "gitlab" }

result = GitlabSchema.execute(query, variables: variables, context: { current_user: current_user })