diff --git a/lib/ci/api/builds.rb b/lib/ci/api/builds.rb
index 2d3d6c75f006ccd16d32da01f1461ecf167e6942..f37a96da93c17cf69fe8356ae0a0a78637aef6b4 100644
--- a/lib/ci/api/builds.rb
+++ b/lib/ci/api/builds.rb
@@ -38,6 +38,8 @@ module Ci
           authenticate_runner!
           update_runner_last_contact
           build = Ci::Build.where(runner_id: current_runner.id).running.find(params[:id])
+          forbidden!('Build has been erased!') if build.erased?
+
           build.update_attributes(trace: params[:trace]) if params[:trace]
 
           case params[:state].to_s
@@ -99,6 +101,7 @@ module Ci
           not_found! unless build
           authenticate_build_token!(build)
           forbidden!('Build is not running!') unless build.running?
+          forbidden!('Build has been erased!') if build.erased?
 
           artifacts_upload_path = ArtifactUploader.artifacts_upload_path
           artifacts = uploaded_file(:file, artifacts_upload_path)
diff --git a/spec/requests/ci/api/builds_spec.rb b/spec/requests/ci/api/builds_spec.rb
index e1042bde8d5011178c3aebe7e447ec7889f1496d..4246ff38fa22ac0512077c46b4c0e7647806bbcc 100644
--- a/spec/requests/ci/api/builds_spec.rb
+++ b/spec/requests/ci/api/builds_spec.rb
@@ -131,39 +131,41 @@ describe Ci::API::API do
     end
 
     describe "PUT /builds/:id" do
-      let(:commit) { FactoryGirl.create(:ci_commit, project: project)}
-      let(:build) { FactoryGirl.create(:ci_build, commit: commit, runner_id: runner.id) }
+      let(:commit) {create(:ci_commit, project: project)}
+      let(:build) { create(:ci_build_with_trace, commit: commit, runner_id: runner.id) }
 
-      it "should update a running build" do
+      before do
         build.run!
         put ci_api("/builds/#{build.id}"), token: runner.token
+      end
+
+      it "should update a running build" do
         expect(response.status).to eq(200)
       end
 
-      it 'Should not override trace information when no trace is given' do
-        build.run!
-        build.update!(trace: 'hello_world')
-        put ci_api("/builds/#{build.id}"), token: runner.token
-        expect(build.reload.trace).to eq 'hello_world'
+      it 'should not override trace information when no trace is given' do
+        expect(build.reload.trace).to eq 'BUILD TRACE'
+      end
+
+      context 'build has been erased' do
+        let(:build) { create(:ci_build, runner_id: runner.id, erased_at: Time.now) }
+
+        it 'should respond with forbidden' do
+          expect(response.status).to eq 403
+        end
       end
     end
 
     describe 'DELETE /builds/:id/content' do
+      let(:build) { create(:ci_build_with_trace, :artifacts, :success) }
       before { delete ci_api("/builds/#{build.id}/content"), token: build.token }
-      let!(:build) { create(:ci_build_with_trace, :artifacts, :success) }
 
-      it 'should respond with valid status' do
+      it 'should erase build content' do
         expect(response.status).to eq 200
-      end
-
-      it 'should remove build artifacts' do
+        expect(build.trace).to be_empty
         expect(build.artifacts_file.exists?).to be_falsy
         expect(build.artifacts_metadata.exists?).to be_falsy
       end
-
-      it 'should remove build trace' do
-        expect(build.trace).to be_empty
-      end
     end
 
     context "Artifacts" do
@@ -209,9 +211,10 @@ describe Ci::API::API do
           end
         end
 
-        context 'token is invalid' do
-          it 'should respond with forbidden'do
-            post authorize_url, { token: 'invalid', filesize: 100 }
+        context 'authorization token is invalid' do
+          before { post authorize_url, { token: 'invalid', filesize: 100 } }
+
+          it 'should respond with forbidden' do
             expect(response.status).to eq(403)
           end
         end
@@ -224,6 +227,15 @@ describe Ci::API::API do
             allow(ArtifactUploader).to receive(:artifacts_upload_path).and_return('/')
           end
 
+          context 'build has been erased' do
+            let(:build) { create(:ci_build, erased_at: Time.now) }
+            before { upload_artifacts(file_upload, headers_with_token) }
+
+            it 'should respond with forbidden' do
+              expect(response.status).to eq 403
+            end
+          end
+
           context "should post artifact to running build" do
             it "uses regual file post" do
               upload_artifacts(file_upload, headers_with_token, false)
@@ -252,7 +264,9 @@ describe Ci::API::API do
             let(:stored_artifacts_file) { build.reload.artifacts_file.file }
             let(:stored_metadata_file) { build.reload.artifacts_metadata.file }
 
-            before { post(post_url, post_data, headers_with_token) }
+            before do
+              post(post_url, post_data, headers_with_token)
+            end
 
             context 'post data accelerated by workhorse is correct' do
               let(:post_data) do