How to delete old lfs files from a fork ?

ccenvcvb
Contributor
December 11, 2024

I have a fork containing 2500 lfs files. 

That is the entire history of  the original repo.

I followed the instructions on  


How can I delete all lfs objects that are not current. I do not want to duplicate the  lfs history of the original repo as those objects are already stored there. ?

Ideally the fork would just use the same lfs objects as the source as there is no reason, other than increasing disk usage, to duplicate the objects.






 

1 answer

1 accepted

0 votes
Answer accepted
Ben
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 12, 2024

Hi @ccenvcvb 

We don't have a feature to bulk delete the LFS files at this moment, but it's possible to delete them via the API. We do have an existing feature request on this, please Vote & Watch so that you'll get an update whenever we have any at:

While we don't have a UI feature to bulk delete the LFS files, it's possible to delete the LFS files via API, and by using the API you can create a script to bulk delete the LFS files.

I do have a script you can use, but do note that we don't officially support this script, so please use it at your own risk and tweak it as you wish.

  1. You need to get the list of the LFS OID files first, so run the following commands in the repositories
    git lfs ls-files -l | awk -F " " '{print $1}' > lfs.txt
    

     This will print and store the LFS OID in text files of lfs.txt

    • Use these commands to list out the files and OID so you can filter out which files to keep or and remove the OID from lfs.txt list
      git lfs ls-files -l 
  2. Once done, copy the following script and save it as .sh. Then ensure that it has all the necessary permission to execute and simply run the commands to bulk delete your LFS files
    #!/bin/bash
    
    file="lfs.txt"
    
    credential="username:app_password"
    
    i=0
    
    while read line || [ -n "$line" ]
    do 
        i=$((i+1))
        echo "File number : $i"
        echo "LFS Object ID (OID) : $line" 
        cmd=$(curl -s -w "\nHTTP Code:  %{http_code}\n" --user $credential -X DELETE "https://api.bitbucket.org/internal/repositories/<Workspacename>/<Repositoriesname>/lfs/$line")
        echo "Response : $cmd"
        echo ""
    done < $file
    
    echo ""
    echo "Total LFS Object ID in $file :" 
    grep -c "" $file
    

I hope this helps

Cheers!

- Ben (Bitbucket Cloud Support)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin Site Admin
TAGS
AUG Leaders

Atlassian Community Events