Delete File From Amazon S3 Bucket. Example In Swift.

This Swift code example will demonstrate how to delete a file from Amazon AWS S3 Bucket.

The code example below will cover:

  • Configure AWSCognitoCredentialsProvider,
  • Set up AWSS3DeleteObjectRequest
  • Perform deleteObject() to delete a file from a bucket

For you to be able to use Amazon AWS S3 Service you will need to set up Amazon SDK for iOS and once it is done import needed libraries into your View Controller. The code below shows how to import required libraries only. Please refer to this guide on how to download and set up Amazon SDK for iOS.

Import Amazon AWS iOS SDK Required Libraries Into Your View Controller

import AWSCore
import AWSCognito
import AWSS3

Delete File From Amazon S3 Bucket. Code Example in Swift.

let credentialsProvider = AWSCognitoCredentialsProvider(
            regionType: AWSRegionType.-- USEast1,
            identityPoolId: "")
        let configuration = AWSServiceConfiguration(
            region: AWSRegionType.-- USEast1,
            credentialsProvider: credentialsProvider)
        AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
        AWSLogger.defaultLogger().logLevel = .Verbose
        let s3Service = AWSS3.defaultS3()
        let deleteObjectRequest = AWSS3DeleteObjectRequest()
        deleteObjectRequest.bucket = "learn-swift" // bucket name
        deleteObjectRequest.key = "0.jpeg" // File name
        s3Service.deleteObject(deleteObjectRequest).continueWithBlock { (task:AWSTask) -> AnyObject? in
            if let error = task.error {
                print("Error occurred: \(error)")
                return nil
            print("Bucket deleted successfully.")
            return nil

Learn iOS Development with these Video Courses

How to Make a Freaking iPhone App - iOS 10 and Swift 3
icon icon

iOS 10 & Swift 3: From Beginner to Paid Professional

The Complete iOS 10 Developer Course - Build 21 Apps

Swift 3 - Learn to Code with Apple's New Language
icon icon

Learn How to Build Mobile Apps for iOS with Swift, PHP and MySQL
icon icon

Follow me on one of your favourite social networks to learn about new video tutorials and code examples:

Twitter: @SwiftVideoBlog
Google Plus:
Facebook: Swift Developer Blog on Facebook