Removing clouds from Sentinel 2 Surface Reflectance in Google Earth Engine?

For Sentinel 2 the QA60 band contains info on whether the pixel is cloudy or not in 10th and 11th bit for opaque and cirrus clouds. So we can check that by checking values that have 1 on 10th and 11th bit or we can use bitwiseAnd to achieve the same.

// Function to mask cloud from built-in quality band
// information on cloud
var maskcloud1 = function(image) {
  var QA60 = image.select(['QA60']);
  var clouds = QA60.bitwiseAnd(1<<10).or(QA60.bitwiseAnd(1<<11);// this gives us cloudy pixels
  return image.updateMask(clouds.not()); // remove the clouds from image
};


Sentinel 2 Surface Reflectance (SR) dataset comes with 2 ways for removing clouds (and the rest of the "bad bits" like cloud shadows, dark pixels, etc). As well as Sentinel 2 TOA dataset it comes with QA60 bit band, but also comes with the "Scene Classification Map" band (SCM) which is not bit encoded but just a classified data (see SCL Class Table here)

Here is a way of applying it:

var cld = require('users/fitoprincipe/geetools:cloud_masks')

var s2SR = ee.ImageCollection('COPERNICUS/S2_SR')
              //filter start and end date
             .filter(ee.Filter.calendarRange(2013,2019,'year'))
             .filter(ee.Filter.calendarRange(1,12,'month'))
             //filter according to drawn boundary
             .filterBounds(ROI)
             .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'greater_than', 50)

var test_image = s2SR.first()

Map.addLayer(test_image, {bands:['B8', 'B11', 'B4'], min:0, max:5000}, 'test image')

var masked = cld.sclMask(['cloud_low', 'cloud_medium', 'cloud_high', 'shadow'])(test_image)
Map.addLayer(masked, {bands:['B8', 'B11', 'B4'], min:0, max:5000}, 'masked')

Argument options for cld.sclMask are: 'cloud_low', 'cloud_medium', 'cloud_high', 'shadow', 'saturated', 'dark', 'cirrus', 'snow' and 'water' (you can also mask out 'vegetation' and 'bare_soil', but take in count that your are masking this out)

You can look at the source code of geetools:cloud_masks here