memcached delete command is used to delete an existing key from the memcached server.
syntax
the basic syntax of memcached delete command is as shown below −
delete key
if the key is successfully deleted, then it returns deleted. if the key is not found, then it returns not_found, otherwise it returns error.
example
in this example, we use tutorialspoint as a key and store memcached in it with an expiration time of 900 seconds. after this, it deletes the stored key.
set tutorialspoint 0 900 9 memcached stored get tutorialspoint value tutorialspoint 0 9 memcached end delete tutorialspoint deleted get tutorialspoint end delete tutorialspoint not_found
delete data using java application
to delete data from a memcached server, you need to use the memcached delete method.
example
import net.spy.memcached.memcachedclient;
public class memcachedjava {
public static void main(string[] args) {
// connecting to memcached server on localhost
memcachedclient mcc = new memcachedclient(new
inetsocketaddress("127.0.0.1", 11211));
system.out.println("connection to server successful");
system.out.println("set status:"+mcc.set("tutorialspoint", 900, "memcached").done);
// get value from cache
system.out.println("get from cache:"+mcc.get("tutorialspoint"));
// delete value from cache
system.out.println("delete from cache:"+mcc.delete("tutorialspoint").isdone());
// check whether value exists or not
system.out.println("get from cache:"+mcc.get("tutorialspoint"));
}
}
output
on compiling and executing the program, you get to see the following output −
connection to server successful set status:true get from cache:memcached delete from cache:true get from cache:null