title: AWS remove files in S3 using CodeBuild in CodePipeline
body: <p>How can I remove unwanted files in an <strong>S3 bucket</strong> as the output of a pipeline in <strong>CodePipeline</strong>, using <strong>CodeBuild</strong>'s <code>buildspec.yml</code> file?</p>
  
  <p>For example:</p>
  
  <p>The <code>build</code> folder of a GitHub repo is put in the designated S3 bucket so the bucket can be used as a static website.</p>
  
  <p>I pushed a file earlier to the bucket which I don't need anymore. How do I use the <code>buildspec.yml</code> file to "clean" the bucket before pushing the artifacts of my pipeline to the bucket?</p>
  
  <p>An example <code>buildspec.yml</code> file:</p>
  
  <pre><code>version: 0.2
  
  phases:
    build:
      commands:
        - mkdir build-output
        - find . -type d -name public -exec cp -R {} build-output \;
        - find . -mindepth 1 -name build-output -prune -o -exec rm -rf {} +
    post_build:
      commands:
        - mv build-output/**/* ./
        - mv build-output/* ./
        - rm -R build-output
  artifacts:
    files:
      - '**/*'
  </code></pre>
  
  <p>Should the command:</p>
  
  <pre><code>rm -rf *
  </code></pre>
  
  <p>in <code>build</code> phase like this?</p>
  
  <pre><code>build:
  
  commands:
    - aws s3 rm s3://mybucket/ --recursive
  </code></pre>
  
  <p>And how do I reference the right bucket instead of hardcoding the name in the file?</p>