Purge devices on reboot or daily using the supervisor API

Hello,

I am trying to purge data on my devices everyday, it seems to improve the performance a lot

I want to use the following code in a start.sh in one of my services
curl -X POST --header “Content-Type:application/json” “$BALENA_SUPERVISOR_ADDRESS/v2/applications/$BALENA_APP_ID/purge?apikey=$BALENA_SUPERVISOR_API_KEY”

It will go in a endless loop everytime i restart

So i was thinking I could check - if( /data is empty){ dont purge}else{purge}

Where is the user applications data folder mentioned in the supervisor docs

Hi,

I think what you’re looking for is /mnt/data

John

Ah my strategy will not work because I am not able to check on the folder from within my “docker” containers

Is there a better way I can programatically purge device data daily?

Hey @PatrickEntabeni,

I’ve been thinking about your workflow a bit and I may have a solution.

Since it sounds like you are already using multiple services, what if you added an additional service to your app with a sole purpose of sleeping for 24-hours before running the supervisor purge?
Something like as the entrypoint script:

#!/bin/sh
sleep 24h
curl -X POST --header "Content-Type:application/json" "$BALENA_SUPERVISOR_ADDRESS/v2/applications/$BALENA_APP_ID/purge?apikey=$BALENA_SUPERVISOR_API_KEY"
# all services restart at this point

This way you avoid the restart loop, but after 24-hours the purge command will be run resulting in a restart of all services, including this one.

Another way would be to manually rm -rf all the named volumes from within an additional service, but that would mean the other services would keep running as the data is being removed, which may or may not work for your app.

Let me know if this solution works for you or or if we should go back to the drawing board!

Take care,
Kyle

1 Like

Thanks @klutchell

Thats an awesome idea, I think the rm -rf will work for me

How do I access the Host container from a child service to delete the /mnt/data folder?

How do I access the Host container from a child service to delete the /mnt/data folder?

You can not access the host’s /mnt/data from a container.

When my colleague said “Another way would be to manually rm -rf all the named volumes from within an additional service”, he meant “mount all of the existing Named Volumes into this service as well and run rm -rf in each one”
Something like:

version: "2.1"

volumes:
  frontendvol:
  backendvol:

services:

  frontend:
    build: ./frontend
    volumes:
      - "frontendvol:/path/to/data"

  backend:
    build: ./unbound
    volumes:
      - "backendvol:/path/to/data"
      
  purge:
    build: ./purge
    command: /purge.sh # sleep 24 hours before rm -rf contents of /data/*/*
    volumes:
      - frontendvol:/data/frontendvol
      - backendvol:/data/backendvol

I personally prefer the “sleep 24h then call the purge endpoint” way.