2

I can delete an object in minio using below command.

from minio import Minio
from minio.error import ResponseError

minioClient = Minio('localhost:9000',
                    access_key='your-key',
                    secret_key='your-key', secure=False)


#Remove an object.
try:
    minioClient.remove_object('test', 'sampletest')
except ResponseError as err:
    print(err)

Here I have a path which contains many objects. Bucket/path/objects, here I want to delete entire path folder. In linux I can issue rm -rf path to remove its content recursevely. How to do the same in Minio using Python client SDK ? I see only api to remove object and remove objects from client sdk. Please advice how to remove it.

remove path

ajayramesh
  • 3,576
  • 8
  • 50
  • 75

2 Answers2

5

to delete an entire folder in minio, you just have to delete all the contents of the folder that you intend to delete.

In your case, 'path' folder would be deleted when you run

minioClient.remove_object('test','path/p1.jpg')
minioClient.remove_object('test','path/scene1.jpg')


To do it in a better way, you can use list_objects with prefix and recursive parameters as follows:

from minio import Minio
from minio.error import ResponseError

minioClient = Minio('localhost:9000',
                    access_key='your-key',
                    secret_key='your-key', secure=False)

def deleteFolder2(bucketname, folderName):
    # Delete using "remove_object"
    objects_to_delete = minioClient.list_objects(bucketname, prefix=folderName, recursive=True)
    for obj in objects_to_delete:
        minioClient.remove_object(bucketname, obj.object_name)


def deleteFolder1(bucketname, folderName):
    # Delete using "remove_objects"
    objects_to_delete = minioClient.list_objects(bucketname, prefix=folderName, recursive=True)
    objects_to_delete = [x.object_name for x in objects_to_delete]
    for del_err in minioClient.remove_objects(bucketname, objects_to_delete):
        print("Deletion Error: {}".format(del_err))


try:
    # Use either deleteFolder1 or deleteFolder2
    deleteFolder1('test', 'path')
    deleteFolder2('test', 'path')
except ResponseError as err:
    print(err)

NOTE:

  • The returned value of remove_objects in deleteFolder1 is an iterator. As the function is lazy and will not evaluate by default. The iterator returned above must be evaluated (for e.g. using a loop) as it is done in deleteFolder1.
  • The iterator will contain items only if there are errors when the service performs a delete operation on it. Each item contains error information for an object that had a delete error.
  • The folderName you pass to deleteFunction should be absolute path i.e., If the bucket structure is as follows test/path/path2 and if you want to delete path2 folder you need to pass path/path2 as folderName to deleteFunction

Reference: MinIO | Python Client API Reference

  • 1
    For anyone landing here now: you need to build a list of DeleteObject (from minio.deleteobjects import DeleteObject) with the name of the object and then passing it to remove_objects. For instance you can modify deleteFolder1 as follow: [DeleteObject(x.object_name) for x in objects_to_delete] – Alexandre Pieroux Nov 10 '22 at 07:50
1

Use pyminio to do it:

from pyminio import Pyminio

pyminio_client = Pyminio.from_credentials(
    endpoint='<your-minio-endpoint>',  # e.g. "localhost:9000/"
    access_key='<your-minio-access-key>',
    secret_key='<your-minio-secret-key>'
)

pyminio_client.rm('/test/sampletest', recursive=True)