Help Center/ Object Storage Service/ SDK Reference/ PHP/ Object APIs/ Multipart Upload/ Clearing Fragments of a Multipart Upload Task
Updated on 2026-09-22 GMT+08:00

Clearing Fragments of a Multipart Upload Task

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see API Reference.

Deleting fragments refers to deleting the parts generated by incomplete multipart uploads. If a multipart upload task is interrupted or fails, unassembled parts are left in the bucket, occupying storage space and incurring fees. You can use the fragment deletion function to delete these unnecessary parts. You are advised to periodically clear multipart upload tasks that have not been completed for more than seven days to avoid unnecessary storage fees.

OBS SDKs provide the following methods for deleting fragments:

  • Single deletion: You can call the API for aborting a multipart upload task to delete the fragments of a single incomplete multipart upload. For details, see Performing a Multipart Upload.
  • Batch deletion: You can call the API for listing multipart upload tasks to list all incomplete multipart uploads and then delete their fragments in batches. For details, see Performing a Multipart Upload.

This example shows how to stop incomplete multipart uploads in batches, list all such tasks in a bucket, and cancel them one by one to clear fragments.

<?php

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use GuzzleHttp\Promise\EachPromise;
// Create an ObsClient instance.
$obsClient = new ObsClient([
    // Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
    //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    'key' => getenv('ACCESS_KEY_ID'),
    'secret' => getenv('SECRET_ACCESS_KEY'),
    // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
    'endpoint' =&gt; "obs.ap-southeast-1.myhuaweicloud.com",
    'signature' => 'obs'
]);

$bucketName = 'bucketname';
$concurrency = 5;

// List all multipart uploads using pagination.
// To clear only fragments with a specified prefix, set the Prefix parameter in listMultipartUploads.
$isTruncated = true;
$keyMarker = null;
$uploadIdMarker = null;
$uploads = [];

while ($isTruncated) {
       $listResp = $obsClient->listMultipartUploads([              'Bucket' => $bucketName,
              'KeyMarker' => $keyMarker,
              'UploadIdMarker' => $uploadIdMarker
       ]);       if (empty($listResp['Uploads'])) {
              break;
       }
       foreach ($listResp['Uploads'] as $upload) {
              // To clear only fragments that have been created for more than seven days, add the following filter criteria:
              // if (strtotime($upload['Initiated']) > time() - 7*86400) continue;
              $uploads[] = ['Key' => $upload['Key'], 'UploadId' => $upload['UploadId'], 'Initiated' => $upload['Initiated']];
       }
       $isTruncated = $listResp['IsTruncated'];
       $keyMarker = $listResp['NextKeyMarker'];
       $uploadIdMarker = $listResp['NextUploadIdMarker'];
}

printf("Found %d uploads to abort\n\n", count($uploads));

// Stop multipart upload tasks concurrently.
$successCount = 0;
$failCount = 0;

$promiseGenerator = function () use ($obsClient, $bucketName, $uploads, &$successCount, &$failCount) {
       foreach ($uploads as $upload) {
              printf("Key:%s, UploadId:%s, Initiated:%s\n", $upload['Key'], $upload['UploadId'], $upload['Initiated']);
              yield $obsClient->abortMultipartUploadAsync (['Bucket' => $bucketName,
                     'Key' => $upload['Key'],
                     'UploadId' => $upload['UploadId']
              ], function ($exception, $resp) use ($upload, &$successCount, &$failCount) {
                     if ($exception === null) {
                            printf("AbortSuccess: Key:%s, RequestId:%s\n", $upload['Key'], $resp['RequestId']);
                            $successCount++;
                     } else {
                            printf("AbortFailed: Key:%s, Status:%d, Message:%s\n", $upload['Key'], $exception->getStatusCode(), $exception->getExceptionMessage());
                            $failCount++;
                     }
              });       }
};

$eachPromise = new EachPromise($promiseGenerator(), [
       'concurrency' => $concurrency
]);

$eachPromise->promise()->wait();

printf("\nResult: success=%d, fail=%d\n", $successCount, $failCount);