Deleting a project manually
Sometimes projects are not completely deleted due to postgres statement timeouts and can end up in a half-deleted state where it cannot be deleted via the admin dashboard.
Preparation
Section titled “Preparation”- You will need to know the ID or path of the project. For example, the path for the runbooks project
https://gitlab.com/gitlab-com/runbooks
will begitlab-com/runbooks
. - You will need a user id or username that has the ability to delete a project, such as the user that created the project or an admin on GitLab.com.
-
Start a rails console issuing the command
sudo gitlab-rails console
. -
Verify that the delete error is due to a hitting a database timeout.
PG::QueryCanceled: ERROR: canceling statement due to statement timeout
. You can do this like so :Project.find_by_full_path('path/to/project').delete_error=> "PG::QueryCanceled: ERROR: canceling statement due to statement timeout\nCONTEXT: SQL statement \"DELETE FROM ONLY \"public\".\"merge_request_diff_files\" WHERE $1 OPERATOR(pg_catalog.=) \"merge_request_diff_id\"\"\n: DELETE FROM \"projects\" WHERE \"projects\".\"id\" = 12345" -
Fetch the username and project:
user = User.find_by_username('user-or-admin-username-goes-here')proj = Project.find_by_full_path('path/to/project')# Alternatively, if you only have the IDsuser = User.find(55555)proj = Project.find(12345) -
Destroy the project’s dependent associations. This might take a while if they have a lot of pipelines or artifacts. You can do this with:
Rails.logger.level = 0proj.destroy_dependent_associations_in_batches(exclude: [:container_repositories]);nil -
Attempt to delete the project via the
DestroyService
to clean up the rest of the project such as the wiki, registry images, etc. You may need to run this more than once if you encounter a statement timeout (it will return false):Projects::DestroyService.new(proj, user, {}).execute=> true -
Verify the project no longer exists:
proj = Project.find_by_full_path('path/to/project')=> nil# Alternatively, if you only have the IDproj = Project.find(12345)=> nil