9

I'm currently using couchbase-lite inside my iOS and android application to sync down files from a database running CouchDB.

Every so often I remove files that are not longer needed, and I would like the same files to be removed from the mobile app as well, but any pull replication only pulls updates or new files, and doesnt trigger a delete on the mobile app.

Is there any way to delete documents from the mobile app that are no longer on the server DB without doing a full purge on the mobile application, and then resyncing the whole database?

Anthony Taylor
  • 3,073
  • 3
  • 21
  • 30
  • Are you removing them on the server side or the mobile side? Replication is designed to pull deletes as well but I can't remember if CouchDB follows the correct procedure. If you delete them on the mobile side, though, the deletes will sync to the server. – borrrden Jun 18 '18 at 05:35
  • @borrrden So its a one way replication. The users syncs the DB to the phone, and doesnt edit anything. Then when I update the info(add info, and remove info) I want that deleted info removed from the users phone as well, as its time dated, so its useless after a certain amount of time. But if I delete data, when the app pulls, it keeps the old info after the pull replication. – Anthony Taylor Jun 18 '18 at 11:34
  • Do you happen to know if CouchDB keeps the revision history intact when you delete it? This would show up as a new revision with the `_deleted` flag. If you are just simply removing the value from the server then it will *not* be replicated. Only 'new revisions' will be replicated, which is the reason for this so-called 'tombstone revision.' – borrrden Jun 18 '18 at 23:03
  • @borrrden I am unsure, I will check and find out. From my understanding, I only overwrite docs, leading to a increased rev number. What I really want is for the mobile app to just stay perfectly synced with the server. – Anthony Taylor Jun 20 '18 at 13:56

2 Answers2

0

From the database class there is a purgeDocument() method. This removes the target document from the local database - server copies of target documents are left unchanged. If the document is later updated then the document will return to the local client on next replication.

http://docs.couchbase.com/mobile/2.1/couchbase-lite-swift/Classes/Database.html

Ian Bradbury
  • 1,416
  • 11
  • 18
0

You can delete documents from database based on id as following:

try {
        for (Result result : docList) {
            String id = result.getString(0);
            Document doc = database.getDocument(id);
            database.delete(doc);
        }
    } catch (CouchbaseLiteException e) {
        e.printStackTrace();
    } 

To get the document id you have to query something like this:

Query query = QueryBuilder
    .select(SelectResult.expression(Meta.id), SelectResult.all())
    .from(DataSource.database(database))

ResultSet rs = query.execute();
    for (Result result : rs) {
        Dictionary data = result.getDictionary("db_name");
        Log.e("CouchbaseLite ", "document: " + data);
        Log.e("CouchbaseLite ", "id: " + result.getString(0));
    }
Uniruddh
  • 4,427
  • 3
  • 52
  • 86