Over time, your datasets may accumulate a large amount of obsolete or unnecessary metadata, such as historical logs, temporary objects, or metadata for deleted source objects. This redundancy consumes storage and reduces retrieval efficiency. This topic explains how to bulk delete unwanted object metadata from a dataset.
Prerequisites
-
You have created an AccessKey.
-
You have activated Object Storage Service (OSS), created a bucket, and uploaded objects to it. For more information, see Upload objects.
-
Intelligent Media Management (IMM) is activated. For more information, see Activate IMM.
-
You have created a project in the IMM console. For more information, see Create a project.
Note-
You can also call the CreateProject API operation to create a project.
-
You can call the ListProjects API operation to list all projects that are created in a specified region.
-
-
You have created a metadata index for your objects based on your use case. For more information, see Create a metadata index.
Usage notes
-
Permission management: The AccessKey you use must have the required permissions to perform operations on the target dataset.
-
Exception handling: The script provides basic exception printing. Implement robust error handling for production environments.
-
Data security: Bulk delete operations are irreversible. Before running the script, confirm the operation's scope to prevent accidental data loss.
Procedure
The Intelligent Media Management (IMM) console does not currently support bulk metadata deletion. You must use the script in this topic to perform this operation.
Step 1: Install the IMM Python SDK
-
Environment requirements
You must use Python 3.7 or later. You can run the following command to check your Python version:
python --version -
Installation
Use the
pipcommand to install a specific version (4.6.2) of the IMM SDK for Python:pip install alibabacloud_imm20200930==4.6.2 -
For more information, see Intelligent Media Management.
Step 2: Configure environment variables
After you create an AccessKey, you must configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.
Step 3: Run the bulk deletion script
Configure and run the code to bulk delete metadata from the dataset.
-
Save the code file.
Save the following sample code as a file named
delete_dataset_file_meta.py.# -*- coding:utf-8-* import os from alibabacloud_imm20200930.client import Client as imm20200930Client from alibabacloud_tea_openapi import models as open_api_models from alibabacloud_imm20200930 import models as imm_20200930_models from alibabacloud_tea_util import models as util_models from alibabacloud_tea_util.client import Client as UtilClient class DeleteDatasetFileMeta: """ Deletes object metadata from a dataset. """ def __init__(self, endpoint): self.endpoint = endpoint def create_client(self): """ Initializes a client by using credentials. @return: Client @throws Exception """ config = open_api_models.Config( # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment. access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment. access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] ) # For more information about endpoints, see https://api.aliyun.com/product/imm. config.endpoint = self.endpoint return imm20200930Client(config) def simple_query(self, project_name, dataset_name, max_result=100, next_token=None): """ Uses the SimpleQuery API operation to query objects in a dataset. :param project_name: :param dataset_name: :param max_result: :param next_token: :return: """ client = self.create_client() simple_query_request = imm_20200930_models.SimpleQueryRequest( project_name=project_name, dataset_name=dataset_name, next_token=next_token, max_results=max_result, with_fields=["URI"] ) runtime = util_models.RuntimeOptions() try: response = client.simple_query_with_options(simple_query_request, runtime) return response.body.to_map() except Exception as error: # This is a basic example of printing and handling exceptions. Implement more robust exception handling for your project. # Error message print(error.message) # Diagnostic address print(error.data.get("Recommend")) UtilClient.assert_as_string(error.message) def batch_delete_file_meta(self, project_name, dataset_name, uri_list): """ Uses the BatchDeleteFileMeta operation to delete metadata of multiple objects in a dataset. :param project_name: :param dataset_name: :param uri_list: :return: """ if not uri_list: return client = self.create_client() batch_delete_file_meta_request = imm_20200930_models.BatchDeleteFileMetaRequest( project_name=project_name, dataset_name=dataset_name, uris=uri_list ) runtime = util_models.RuntimeOptions() try: response = client.batch_delete_file_meta_with_options( batch_delete_file_meta_request, runtime) return response.body.to_map() except Exception as error: # This is a basic example of printing and handling exceptions. Implement more robust exception handling for your project. # Error message print(error.message) UtilClient.assert_as_string(error.message) @staticmethod def main(endpoint, project_name, dataset_name): """Deletes metadata from the dataset.""" tool = DeleteDatasetFileMeta(endpoint) next_token = None while True: # Uses SimpleQuery to iteratively query objects in the dataset. simple_query_response = tool.simple_query( project_name, dataset_name, max_result=100, next_token=next_token) next_token = simple_query_response.get("NextToken") uri_list = [x.get("URI") for x in simple_query_response.get("Files", list())] # Delete the metadata in bulk. delete_response = tool.batch_delete_file_meta(project_name, dataset_name, uri_list) print(f"Deleting objects: {uri_list}, response: {delete_response}") if not next_token: break print("Deletion complete.") if __name__ == "__main__": """ 1. You must install the IMM SDK for Python. For more information, see https://next.api.aliyun.com/api-tools/sdk/imm?spm=a2c4g.11186623.0.0.5e9952feu0Zm3a&version=2020-09-30&language=python-tea&tab=primer-doc. 2. The script calls the SDK by using an AccessKey ID and AccessKey secret. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are set in your runtime environment. 3. Replace the endpoint, project_name, and dataset_name parameters with your actual values. """ # For more information about endpoints, see https://api.aliyun.com/product/imm. endpoint = 'imm.cn-hangzhou.aliyuncs.com' # IMM project name project_name = "test-project" # Dataset name dataset_name = "test-dataset" DeleteDatasetFileMeta.main(endpoint, project_name, dataset_name) -
Modify the configuration parameters.
Replace the
endpoint,project_name, anddataset_nameparameters with your actual values.# For more information about endpoints, see https://api.aliyun.com/product/imm endpoint = 'imm.cn-hangzhou.aliyuncs.com' # IMM project name project_name = "test-project" # Dataset name dataset_name = "test-dataset" -
Run the following command to start the bulk deletion.
python delete_dataset_file_meta.py -
View the result.
During script execution, the script prints the URIs of the objects deleted in each batch and their corresponding responses. For example:
Deleting objects: ['oss://xxx/group/3.jpg', 'oss://xxx/group/10.jpg'], response: {'RequestId': '019A53C8-22D6-5FD0-8E3B-C9D5C3680838'} Deleting objects: ['oss://xxx/group/9.jpg', 'oss://xxx/group/20.jpg'], response: {'RequestId': '14107119-9B60-5B21-A785-1D4F509FE778'} Deleting objects: ['oss://xxx/group/17.jpg', 'oss://xxx/group/18.jpg'], response: {'RequestId': '5EACC355-2E05-5EF0-8056-24FFB82641AF'} Deleting objects: ['oss://xxx/group/5.jpg', 'oss://xxx/group/6.jpg'], response: {'RequestId': '2BB8975B-9BFE-5050-B626-FFDB714870D2'} Deleting objects: ['oss://xxx/group/16.jpg', 'oss://xxx/group/1.jpg'], response: {'RequestId': '8F672749-58A5-520F-B7D6-F1A5F43A1C51'} Deleting objects: ['oss://xxx/group/2.jpg', 'oss://xxx/group/12.jpg'], response: {'RequestId': '8DD5BEDE-0191-5D95-AFE8-5AB703873708'} Deleting objects: ['oss://xxx/group/19.jpg', 'oss://xxx/group/4.jpg'], response: {'RequestId': 'EE9FAF93-55F8-55C6-B0B7-921D359657FF'} Deleting objects: ['oss://xxx/group/8.jpg', 'oss://xxx/group/14.jpg'], response: {'RequestId': 'B40EB593-546C-5508-AB90-5ACC25AA1587'} Deleting objects: ['oss://xxx/group/11.jpg', 'oss://xxx/group/15.jpg'], response: {'RequestId': '7CCA13E5-02E3-5348-B06D-D6D050EFE81E'} Deleting objects: ['oss://xxx/group/13.jpg', 'oss://xxx/group/7.jpg'], response: {'RequestId': '4F960D2D-400C-5DDC-8CDE-29F72893DCD6'}