You can delete one or more objects that you no longer need.
Deleted objects cannot be recovered. Exercise caution when you delete objects.
Usage notes
When you use packaging tools such as Webpack and Browserify, install OSS SDK for Browser.js by running the npm install ali-oss command.
If you want to access an OSS bucket from a browser but no CORS rules are configured for the bucket, the browser rejects the request. Therefore, you must configure CORS rules for a bucket if you want to access the bucket from a browser. For more information, see Installation.
In most cases, OSS SDK for Browser.js is used in browsers. To prevent your AccessKey pair from being exposed, we recommend that you use temporary access credentials obtained from Security Token Service (STS) to access OSS.
The temporary access credentials consist of an AccessKey pair and a security token. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to obtain temporary access credentials, see Use STS for temporary access authorization.
Delete a single object
The following code provides an example on how to delete a single object:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id="delete">Delete</button>
<!-- Import the SDK file -->
<script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"></script>
<script type="text/javascript">
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourRegion',
authorizationV4: true,
// Specify the temporary AccessKey pair obtained from STS. The AccessKey pair consists of an AccessKey ID and an AccessKey secret.
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
// Specify the security token that you obtained from STS.
stsToken: 'yourSecurityToken',
// Specify the name of the bucket. Example: examplebucket.
bucket: "examplebucket",
});
const deleteSingle = document.getElementById("delete");
// Delete a single object.
deleteSingle.addEventListener("click", async () => {
// Specify the name of the object that you want to delete. Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
let result = await client.delete('exampledir/exampleobject.txt');
console.log(result);
});
</script>
</body>
</html>
Delete multiple objects
The following code provides an example on how to delete multiple objects:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id="deleteAll">Delete All</button>
<!-- Import the SDK file -->
<script
type="text/javascript"
src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"
></script>
<script type="text/javascript">
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourRegion',
authorizationV4: true,
// Specify the temporary AccessKey pair obtained from STS. The AccessKey pair consists of an AccessKey ID and an AccessKey secret.
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
// Specify the security token that you obtained from STS.
stsToken: 'yourSecurityToken',
// Specify the name of the bucket. Example: examplebucket.
bucket: "examplebucket",
});
const deleteAll = document.getElementById("deleteAll");
// Delete multiple objects.
deleteAll.addEventListener("click", async () => {
// Specify the names of the objects that you want to delete. Specify the full path of the objects. Do not include the bucket name in the full path.
let result = await client.deleteMulti([
"example.txt",
"exampleobject.txt",
"newexampleobject.txt",
]);
console.log(result);
result = await client.deleteMulti(
["example.txt", "exampleobject.txt", "newexampleobject.txt"],
{
// Set the quiet parameter to specify whether to list all objects that are deleted. If you set quiet to true, OSS does not return the list of the objects that are deleted. If you set quiet to false, OSS returns the list of the objects that are deleted.
quiet: true,
}
);
});
</script>
</body>
</html>
References
Delete a single object
For the complete sample code that is used to delete an object, visit GitHub.
For more information about the API operation that you can call to delete an object, see DeleteObject.
Delete multiple objects
For the complete sample code that is used to delete multiple objects, visit GitHub.
For more information about the API operation that you can call to delete multiple objects, see DeleteMultipleObjects.