Method to purge individual user data on couchbase server based on their associated channel

I have a mobile app that uses couchbase lite for local storage. A couchbase server instance is used as a backup/restore repository. Each mobile user’s data is backed up by doing a push to the server where each user’s data is a unique channel. Each document in the user’s database also has a field identifying the channel. User data restore is a pull from the remote server after purging all of the local documents. The server also has a node.js web service for user management via the Sync Gateway Admin REST Api.

I’d like to add functionality to 1) check if there’s is previously backed up data on the server for a given user and 2) clear the backed up data if the user chooses to do so.

What are the mechanism to do so? I checked the Sync Gateway REST Api and Admin Rest API and i didn’t see any that would be able to do that. How can’t I check if a specific channel exists or delete a specific channel?

Would I need to use the Couchbase Node.js SDK in my node.js web service and connect to the Couchbase server directly and query the documents.

There isn’t an automated way to tombstone all documents for a given channel.

You can identify whether there’s data on the server using your Admin REST API client - issuing a _changes request with a channel filter (setting filter=sync_gateway/bychannel, channels=[your_channel]). To clear the backed up data, you’d need to iterate over the result set and issue a delete via the Sync Gateway REST API.

That’s only going to tombstone the data, and not do a complete purge. In future we’re planning to add purge support to Sync Gateway (see https://github.com/couchbase/sync_gateway/issues/1219), but it’s not there yet.

@adamf I am not understanding your recommendation on “issuing a changes request with a channel filter”? I’m looking at the Sync Gateway Admin REST API documentation and don’t see anything related to what you’re recommending.

I did find a _changes request in the Couchbase Lite REST API documentation (http://developer.couchbase.com/documentation/mobile/1.1.0/develop/references/couchbase-lite/rest-api/database/index.html). Would this also work on the server side via the Sync Gateway REST API? To clarify, I’m not trying to purge documents in the local database. I am trying to purge documents in the server database when the mobile user via a mobile app chooses to do so.

Apologies - I’d meant to include a link to the appropriate documentation:
http://developer.couchbase.com/documentation/mobile/1.1.0/develop/references/sync-gateway/rest-api/database/get-changes/index.html

You could do the same thing via the public REST API as a user (to support triggering this from the client).

@adamf This can also double as a method to check if there are data for a given channel as well? by checking if the result of the request contains any data for a given channel filter?

If the _changes request comes back with no data for the channel when calling over the Admin REST API, there’s no data for a given channel.

When issuing the call over the public REST API returns no data, there are two possibilities - that there is no data for the channel, or that the user issuing the request hasn’t been granted access to that channel.

A channel doesn’t really have an independent existence. It exists if and only if there are documents in it. (Think of it as being like a hashtag in Twitter.) If you delete all the docs in a channel, or otherwise modify those docs so that they’re not in that channel anymore, then the channel will cease to exist.

@jens To clarify the original post, I’m looking for a method to check 1) if there are existing documents for a given channel and 2) a method to delete all documents in a given channel.

The use case I have is using couchbase push/pull as a backup and restore mechanism. In a user restore scenario, I would like to check if there are any documents to restore from before I do a pull request. I need a way to determine if a back up exists (i.e. if there are documents in the user’s channel) to restore from.

Additionally, there will be cases where a restore point has expired or needs to be cleaned up. In that case, I would like to delete or purge all documents belonging to a specific channel. The next time the user wants to restore, I would again need to check if there is a valid restore point to restore from.

I think the change request and filter by channel REST call will work for what I’m looking for. The only issue I see is making sure that the deleted documents in the server don’t get replicated to the local database as deleted documents as well.