Search code examples
pythonazure-functionsazure-web-app-service

How to delete the files stored in Azure App Service storage using Azure Functions?


I have a RAG system that will fetch certain files from an external storage and push to an in-memory ChromaDB after converting them to embeddings. Now the files that can be fetched depends on the user. There will be a unique directory for individual user that persists the data for the this vector database. I am deploying this system using the Azure App service. My requirement is that a specific user's directory should be deleted within 72 hours if no further question is being asked by him or her. For the same, I plan to use Azure Functions Timer trigger that checks through the directory creation date once every 24 hours. Please let me know how I can access the App Service storage using Functions.

I tried using Blob storage for storing the data, but I'd like to store in within the App Service's memory.


Solution

  • Kudu API can be used to delete files or directory. https://webappname.scm,azurewebsites.net/api/vfs/<path/to/directory>

    Below code worked for me.

    I used Http trigger to delete the Vivek directory in site/wwwroot.

    Note :-

    1. used ?recusive=true to delete all files inside the folder or directory.
    2. username and password is available in publish profile.
    3. `DefaultAzureCredential() from azure.identity can be used for credentials

    function_app.py:

    import azure.functions as func
    import requests
    import base64
    import logging
    import json
    
    app = func.FunctionApp()
    
    
    @app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS, methods= ['get'])
    def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
    
        app_name="flask9july"
        username="xxxxxxxxxx"
        password="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        try:
            kudu_api_url = f"https://{app_name}.scm.azurewebsites.net/api/vfs/site/wwwroot"
            credentials = base64.b64encode(f"{username}:{password}".encode('utf-8')).decode('utf-8')
            headers = {
                "Authorization": f"Basic {credentials}"
            }
            before_response = requests.get(kudu_api_url,headers=headers)
            before_status= before_response.status_code
            before_data = before_response.json()
    
            before_response_json = {
                        "data": before_data
                    }
            #deleteting files or folders        
            delete = requests.delete(f"{kudu_api_url}/Vivek/?recursive=true",headers=headers)
    
            after_delete = requests.get(kudu_api_url,headers=headers)
            after_status= after_delete.status_code
            data = after_delete.json()
    
            after_data ={
    
                "data": data
            }
            return func.HttpResponse(f"before delete list: \n{json.dumps(before_response_json)} \n\nDeleted sucessfully \n\nAfter delete list: \n{json.dumps(after_data)}", mimetype="application/json", status_code=200)
    
        except Exception as e:
            return func.HttpResponse(f"Error: {str(e)}")
    

    OUTPUT:

    Folder Vivek is deleted