How to delete firebase data after "n" days

I want do delete some old data from my Firebase, and I have a hard time do make it work.

I tried this solution found here: Firebase chat - removing old messages

but this isn't deprecated.

so I try it this way:

ChildEventListener childEventListener = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -30);

        Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());

        String key = dataSnapshot.getKey();

        Marker retrievemarker =dataSnapshot.getValue(Marker.class);
        Calendar t3 = new           
        GregorianCalendar
        (c.get(Calendar.YEAR),
         c.get(Calendar.MONTH),
         retrievemarker.getSavedate());

        int date1= calendar.get(Calendar.DATE);
        int date2= t3.get(Calendar.DATE);

        Log.d("date1",""+date1);
        Log.d("date2",""+date2);

        if( date1 == date2 ){

            myRef.child(key).setValue(null);

        }

for explanation: In my Firebase I save a object named Marker, this object got a variable to store the savedate, the date where the object was saved in the Firebase.

int date = c.get(Calendar.DATE);
marker.setSavedate(date);

I want to delete the object in the Firebase after 30 days. So I try to subtract 30 days from the Date Today than i want to compare this with the savedate from the Object if it equals i will delete the object from the Firebase.

For the Subtraction I refer to an answer from the following question: How to subtract X days from a date using Java calendar?

But it doesn´t work. If I add a Object today to the Firebase the two dates are always equal. So I guess the subtraction doesn´t work.

What am I doing wrong?


Solution 1:

Say that you have a data structure with nodes line this:

-KItqNxLqzQoLnUCb9sJaddclose
  time: "Thu Apr 28 17:12:05 PDT 2016"
  timestamp: 1461888725444

Each such node has a timestamp property that indicates when it was created. Preferably you'd set this property using Server Timestamp.

With this data structure, you can easily build a query that returns only the items older than 30 days and removes them:

long cutoff = new Date().getTime() - TimeUnit.MILLISECONDS.convert(30, TimeUnit.DAYS);
Query oldItems = ttlRef.orderByChild("timestamp").endAt(cutoff);
oldItems.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot itemSnapshot: snapshot.getChildren()) {
            itemSnapshot.getRef().removeValue();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

Solution 2:

My Firebase structure is like -> firebaseurl.com/test/user_id/2017-05-12 and then my data is structured with time So I am using

long cutoff = new Date().getTime()-TimeUnit.MILLISECONDS.convert(10, TimeUnit.DAYS);

//Get days number for delete data
Date d = new Date(cutoff );
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd");
String old_date = `dateFormatGmt.format(new Date(cutoff));

// Get date of deleting content
ref2 = new Firebase("firebaseurl.com/test/user_id/" + user_id);    // Firebase url
ref2.child(old_date).removeValue();    //Remove element

It works for me