File search API : delete prevailing contents from the store

Have you guys tried the Google File search api?

I find it useful and have implemented in my daily life. How to delete the prevailing data?
Do anyone have any idea on the subject? DOCUMENTATION This Contains some insight but seems to get an error.

def list_documents(store_name):

print("\\n Listing documents in store...")



try:

    pager = client.file_search_stores.documents.list(

        parent=store_name

    )

except Exception as e:

    print(f"Error listing documents: {e}")

    return \[\]



docs = \[\]



for doc in pager:  

    print(" -", doc.name)

    docs.append(doc.name)



if not docs:

    print(" No documents found.")



return docs

**
this seems to return the files as a list.**

def list_and_delete_documents(store_name):

print("\\n This will DELETE ALL documents in the store.")

print("This cannot be undone.")

confirm = input("Type 'YES' to continue: ")



if confirm.strip().upper() != "YES":

    print(" Delete cancelled.")

    return



print("\\n Fetching documents...")



try:

    pager = client.file_search_stores.documents.list(

        parent=store_name

    )

except Exception as e:

    print(f" Error listing documents: {e}")

    return



docs = \[\]

for doc in pager:

    docs.append(doc.name)



if not docs:

    print(" No documents found.")

    return



print("\\n Deleting documents:\\n")



for doc_name in docs:

    try:

        client.file_search_stores.documents.delete(

            name=doc_name,

            force=True

        )

        print(f" Deleted: {doc_name}")

    except Exception as e:

        print(f" Failed to delete {doc_name}: {e}")



print("\\n Deleted successfully!")

when i tried this, error occurs.

Can someone help me out over here?

Thanks. Happy coding!

Can you define more clearly what your intentions behind the phrase ā€œprevailing contentsā€ are?

Do you mean: keep the ID, but remove all contents?

It would seem easier just to delete the fileSearchStore entirely, and use the ā€œforceā€ query parameter to remove any document artifacts if not empty.

Deleting a file search store (optional force)

delete https://generativelanguage.googleapis.com/v1beta/{name=fileSearchStores/your_id}?force=true

Deleting a file document resource

The API itself has no DELETE method specifically for multiple documents in a store. This would have to be an SDK helper that iterates through a list (like your loop).

delete https://generativelanguage.googleapis.com/v1beta/{name=fileSearchStores/*/documents/*}

The asterisks in the query is a confusion layer for what actually goes in ā€œname=ā€ - fileSearchStores/{filesearchstore}/documents/{document}

However, you will note that any call to clear a single listed document needs both the store ID and the document. The SDK method you show I can already tell will fail without even looking, because you aren’t passing both of these, and that is not a path which takes a ā€œforceā€.

The file_search_stores Python ā€œmethodsā€ you show, which might be inferred as existing from the camelcase ā€œmethodā€ in API docs, would need a recent update of the Google API SDKs that would have such support, instead of the ease of just directly making https API calls. ā€œerror occursā€ - you can read the error, and see if there is even such a delete method.

You’ll only find file_search_stores without links in the documentation: Submodules - Google Gen AI SDK documentation

2 Likes

Oh thank you for the insights Jay.

I got solution for the problem. It is actually better to delete the entire store rather than a single file.

    client.file_search_stores.delete(

        name=store_name,

        config={"force": True} 

                        )

This seems to do the trick. What I’m concerned about is what happens if I ever need to delete a specific file by name, or remove a group of files based on a particular upload time range.

The same place you discovered client.file_search_stores.documents.list() - see if there is API reference for a .documents.delete() that surfaces.

You will have to guess the correct fields for the store name and document name if you are also guessing at the method and like me can’t find documentation (or you can go into the latest messy API SDK code and see what they have provided but not documented).

Otherwise, DELETE is an API method that doesn’t take an encoded body, just send the URL with path and your Bearer authentication header. I documented it above.

Ask Gemini to write you a direct call getting GEMINI_API_KEY and using httpx in Python. Oh, I did that for you (untested, so this is a test of the AI to understand): https://aistudio.google.com/app/prompts/1yCrJnbQr2rcympmVihmozxRFuGjeVtBo

Yes thanks for your help !

client.file_search_stores.documents.delete(

    name=file_name,

config={ā€˜force’: True}

)