Support Topics Documentation Slack YouTube Blog

Delete All objects and files After certain amount of time

Hello, I have a table with objects and each object have serveral images in the file storage api, i want each object to be deleted and also all its images from the file storage after one month from being added?
language : java

Hello @Mohammed_Abdalla.

There are no such instructions in our SDK, you can implement it using Timers (for example, timers).

Regards, Nikita.

yes i know, this example


is exactly what i was looking for, but i am not familiar with javascript.

colud you help me convert this example to java

Okey, wait a moment

Hello @Mohammed_Abdalla

For Java:

  1. Create Timer:

  2. Download this Timer

  3. Add code:

    @BackendlessTimer("{'startDate':1595925141240,'language':'JAVA','mode':'DRAFT','model':'default','frequency':{'schedule':'daily','repeat':{'every':1}},'timername':'expireOldActivity'}")
        public class ExpireOldActivityTimer extends com.backendless.servercode.extension.TimerExtender
        {
          @Override
          public void execute()
          {
            List<Map> users = Backendless.Data.of( "Users" ).find();
            List<String> userIds = users.stream()
                    .map( userMap -> (String) userMap.get( "objectId" ) )
                    .collect( Collectors.toList() );
            userIds.forEach( userId -> {
              DataQueryBuilder queryBuilder = DataQueryBuilder.create();
              queryBuilder.setWhereClause( "user.objectId = '" + userId + "'" );
              queryBuilder.setSortBy( "created desc" );
              queryBuilder.setOffset( 2 ); // here you can set your own value - how much objects you need to keep
              queryBuilder.setPageSize( 1 ); // no need to retrieve more, we only need the first object to be removed
              Backendless.Data.of( "ActivityRecord" ).find( queryBuilder ) // find
                      .stream()
                      .findAny()
                      .ifPresent( activity ->
                                          Backendless.Persistence.of( "ActivityRecord" )
                                                  .remove( "created <= '" + activity.get( "created" ) + "' and user.objectId = '" + userId + "'" ) ); // delete obsolete
            } );
          }
        }

thanks

hey, i tried to implement your method but when i choose java from timer’s languages it shows this message : " this language is for read only"

You have to download Coderunner, write your code there and deploy it after.
Please read documentation, everything is explained clear there.
https://backendless.com/docs/bl-java/bl_coderunner.html

  1. Developer downloads and installs the Backendless CodeRunner SDK. Using an IDE developer writes the service code. The compiled code is passed to CodeRunner™, which is a command line utility used for local debugging. Developer can optionally attach to the CodeRunner™ process to set breakpoints in the code for step-by-step debugging.
  2. CodeRunner™ registers with the Backendless Cloud Service. It instructs Backendless to route the service invocation requests to the developer computer for the debugged code.
  3. Client application sends a service API request. Backendless determines if there is a service registered for the received API request and routes it to CodeRunner™.
  4. CodeRunner™ receives the request and executes the code. Developer can inspect and debug invocations locally before the custom code is deployed to the production environment.

Regards,
Stanislaw

ok, thanks