jenkinsautomation

Is this possible to configure Jenkins to clean up old build workspace data?


I believe our devops has properly configured Jenkins jobs to clean up and delete workspace data. I can see we have the following configuration

enter image description here

However, this doesn't help and our project space is growing up (up to 400 GB).

I don't understand why this is happening. Shouldn't this setting control this and delete old build workspaces data?

We never had such a problem for github-actions or gitlab. I don't understand why this is so complex issue for Jenkins to clean up the space.

Is there any way I could quickly delete all old build or even automate this?


Solution

  • You're dealing with a common issue in Jenkins where workspace data isn’t being cleaned up, despite having configurations to do so.

    Jenkins allows you to configure workspace cleanup in a few ways, such as via job-specific settings or through global configurations.

    If you have configured the "Discard Old Builds" setting, it should help control the number of builds retained. However, this primarily handles deleting build records, not necessarily the workspaces or artifacts associated with those builds.

    The workspace directory contains files used during a build, which can be substantial depending on the type of job (e.g., large codebases, dependencies, etc.).

    Even after the "Discard Old Builds" policy is applied, old workspace data might persist, especially if the cleanup strategy doesn't target these. Build Data vs. Workspace Data: The "Discard Old Builds" option only deletes old build records. If workspaces aren’t configured to be cleaned up, they can continue to accumulate data.

    Multibranch Pipelines and Large Repositories: In Jenkins, multibranch pipelines can create separate workspaces for each branch. These can become large, especially for branches that aren't cleaned up.

    You can use the Workspace Cleanup Plugin to automatically delete workspace data after each build or configure specific cleanup criteria. Install the plugin and add a post-build action to clean the workspace. This ensures that each time a build completes, its workspace is deleted.

    Global Configuration – System-Wide Workspace Cleanup: You can configure Jenkins to automatically delete workspaces across all jobs, including those that have been abandoned: Go to Manage Jenkins > Configure System > Workspace Cleanup section. Here, you can define system-wide settings to remove old or unused workspaces.

    My quickest fix-up, you could use a script that periodically runs a cron job to delete old workspaces. The script might look something like this:

    find /var/lib/jenkins/workspace -type d -mtime +7 -exec rm -rf {} \;
    

    This command will delete workspace directories older than 7 days. Adjust the path and age criteria as needed.

    For each job, ensure that you have enabled workspace cleanup. This might need to be done under the "Build Environment" or post-build actions depending on your Jenkins setup.

    There are a whole lot more ways to keep Jenkins in check, but these are some I have implemented and work for me.

    Good luck!