title: Deleting folder from github using octokit/rest
body: <p>I'm trying to use <code>octokit/rest</code> to remove a directory programaticcaly. I have the following code</p>
  <pre><code>import {Octokit as Github} from '@octokit/rest';
  
  const githubToken = &quot;read from vault&quot;
  
  const getCurrentCommit = async (
      github: Github,
      org: string,
      repo: string,
      branch: string = 'main'
  ) =&gt; {
    const {data: refData} = await github.git.getRef({
      owner: org,
      repo,
      ref: `heads/${branch}`,
    })
    const commitSha = refData.object.sha
    const {data: commitData} = await github.git.getCommit({
      owner: org,
      repo,
      commit_sha: commitSha,
    })
    return {
      commitSha,
      treeSha: commitData.tree.sha,
    }
  }
  
  const createNewCommit = async (
      github: Github,
      org: string,
      repo: string,
      message: string,
      currentTreeSha: string,
      currentCommitSha: string
  ) =&gt; {
    const {data} = await github.git.createCommit({
      owner: org,
      repo,
      message,
      tree: currentTreeSha,
      parents: [currentCommitSha],
    });
  
    return data;
  }
  
  const setBranchToCommit = (
      github: Github,
      org: string,
      repo: string,
      commitSha: string,
      branch: string = `main`
  ) =&gt; github.git.updateRef({
    owner: org,
    repo,
    ref: `heads/${branch}`,
    sha: commitSha,
  });
  
  const getdirectorySha = async (
      tree: { path?: string, sha?: string }[],
      directoryName: string
  ) =&gt; {
  
    return tree
    .filter(({path: directoryPath}) =&gt; directoryPath ? directoryPath.includes(directoryName) : false)
    .map(({sha}) =&gt; sha)
    .filter(sha =&gt; sha !== undefined).values().next().value
  };
  
  const deleteFromRepo = async (
      github: Github,
      org: string,
      repo: string,
      directoryName: string,
      branch: string = `main`
  ) =&gt; {
    const currentCommit = await getCurrentCommit(github, org, repo, branch);
  
    const {data: treeData} = await github.git.getTree({
      owner: org,
      repo,
      tree_sha: currentCommit.treeSha
    })
  
    const directorySha = await getdirectorySha(treeData.tree, directoryName);
  
    if (!directorySha) {
      throw new Error(`Could not find an directory '${directoryName}'`)
    }
    
    // Skip the files in the directory I need to remove
    treeData.tree = treeData.tree
    .filter(({path: directoryPath}) =&gt; directoryPath &amp;&amp; !(directoryPath.includes(directoryName)))
    .map(({path: directoryPath, sha}) =&gt; {
      return {path: directoryPath, sha}
    });
  
    // For some reason, this repopulates the files I just skipped
    const commitMessage = `Deleting '${directoryName}' files.`
    const newCommit = await createNewCommit(
        github,
        org,
        repo,
        commitMessage,
        treeData.sha,
        currentCommit.commitSha
    )
    await setBranchToCommit(github, org, repo, newCommit.sha, branch)
  }
  
  const checkRepositoryExists = (
      organisationRepos: any,
      repoName: string) =&gt; {
    if (!organisationRepos.data.map((repo: { name: any }) =&gt; repo.name).includes(repoName)) {
      let errorMessage = `Repo '${repoName}' does not exist`;
      console.log(errorMessage);
      throw new Error(errorMessage);
    }
  };
  
  export const deletedirectoryFromGithubRepo = async (
      directoryName: string
  ) =&gt; {
    const github = new Github({auth: githubToken})
    const organization = `myOrg`
    const repoName = `myRepo`
    const organisationRepos = await github.repos.listForOrg({org: organization})
  
    checkRepositoryExists(organisationRepos, repoName);
  
    await deleteFromRepo(github, organization, repoName, directoryName)
  }
  </code></pre>
  <p>When I call <code>deleteDirectoryFromGithubRepo</code>, it commits an empty commit to github. That is, it created a commit but without any changes in it.</p>
  <p>I read that passing the current commit might be repopulating the deleted files. But removing it results in an error <code>Update is not a fast forward</code></p>
  <p>Full reponse from github</p>
  <pre><code>{
    &quot;name&quot;: &quot;HttpError&quot;,
    &quot;status&quot;: 422,
    &quot;response&quot;: {
      &quot;url&quot;: &quot;https://api.github.com/repos/myOrg/myRepo/git/refs/heads%2Fmain&quot;,
      &quot;status&quot;: 422,
      &quot;headers&quot;: {
        &quot;header1&quot;: &quot;*&quot;,
        &quot;header2&quot;: &quot;*&quot;
      },
      &quot;data&quot;: {
        &quot;message&quot;: &quot;Update is not a fast forward&quot;,
        &quot;documentation_url&quot;: &quot;https://docs.github.com/rest/reference/git#update-a-reference&quot;
      }
    },
    &quot;request&quot;: {
      &quot;method&quot;: &quot;PATCH&quot;,
      &quot;url&quot;: &quot;https://api.github.com/repos/myOrg/myRepo/git/refs/heads%2Fmain&quot;,
      &quot;headers&quot;: {
        &quot;accept&quot;: &quot;application/vnd.github.v3+json&quot;,
        &quot;user-agent&quot;: &quot;octokit-rest.js/18.12.0 octokit-core.js/3.6.0 Node.js/14.19.3 (linux; x64)&quot;,
        &quot;authorization&quot;: &quot;token [REDACTED]&quot;,
        &quot;content-type&quot;: &quot;application/json; charset=utf-8&quot;
      },
      &quot;body&quot;: &quot;{\&quot;sha\&quot;:\&quot;75dd3**************ee3da188d4\&quot;}&quot;,
      &quot;request&quot;: {}
    }
  }
  </code></pre>
  <p>I'm using <code>octokit-rest.js/18.12.0</code></p>
  <p>Any idea how to fix this?</p>