diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index 3e67b2771c1d7322328286c2c88b23edc822509e..d7fccb2197d776e788fd2f960ed1ed1c5bcee34d 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -54,6 +54,8 @@ module Ci
     # To prevent db load megabytes of data from trace
     default_scope -> { select(Ci::Build.columns_without_lazy) }
 
+    before_destroy { project }
+
     class << self
       def columns_without_lazy
         (column_names - LAZY_ATTRIBUTES).map do |column_name|
@@ -145,10 +147,6 @@ module Ci
       end
     end
 
-    def project
-      commit.project
-    end
-
     def project_id
       commit.project.id
     end