Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save thomaschaplin/5219d908838db7015787dc1c628620a3 to your computer and use it in GitHub Desktop.

Select an option

Save thomaschaplin/5219d908838db7015787dc1c628620a3 to your computer and use it in GitHub Desktop.
How to delete an index.lock file in Jenkins

How to delete a index.lock file via the Script Console

Steps

Step 1

In the console output of the failing job you will see an error similar to - stderr: fatal: Unable to create '/var/jenkins_home/jobs/job-name-example/workspace@script/.git/index.lock': File exists.

Step 2

Take a note of the path from step 1 - /var/jenkins_home/jobs/job-name-example/workspace@script/.git/index.lock

Step 3

Navigate to the Script Console in Jenkins UI - https://jenkins.example.com/script

Step 4

Paste the following code block into the console for the values you found in step 2 without /index.lock.

dh = new File('./var/jenkins_home/jobs/job-name-example/workspace@script/.git')
dh.eachFile {
    println(it)
}

Step 5

Click Run

Step 6

You will see an output of all the files in the .git directory like below...

./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/index.lock
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/config
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/lfs
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/branches
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/objects
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/refs
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/FETCH_HEAD
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/index
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/logs
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/HEAD
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/hooks
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/info
./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/description

Step 7

Ensure the file exists - ./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/index.lock

Step 8

Paste the following code block into the console for the values you found in step 2.

String filename = "./var/jenkins_home/jobs/jenkins-job-example/workspace@script/.git/index.lock"  
// this deletes the file 
boolean fileSuccessfullyDeleted =  new File(filename).delete()  
println fileSuccessfullyDeleted

Step 9

Click Run and you should see the output - true

Step 10

Run step 4, 5 & 6 again and ensure the index.lock file has been deleted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment